Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:17+00:00 2026-05-17T00:07:17+00:00

How is the conditional operator represented using bitwise operators? It is a homework question

  • 0

How is the conditional operator represented using bitwise operators?

It is a homework question where I have to implement the conditional operator using only bitwise operations. It would be simple if if statements were allowed, however it has to be strictly bitwise operators.

Only the operators !, ~, &, ^, |, +, >>, and << can be used. No if statements or loops can be used.

The function takes three ints and works just like the normal conditional operator. The first argument is evaluated as either zero or non-zero. If the first argument is zero then the second argument is returned. If the first argument is non-zero then the third argument is returned.

I was hoping there would be a simple algorithm for this. Any ideas on where to start would be a great help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:07:18+00:00Added an answer on May 17, 2026 at 12:07 am

    Are shifts allowed as bitwise operators? Are arithmetic operators allowed?

    Your edit is not entirely clear, but I assume that you need to implement an equivalent of

    a ? b : c
    

    where a, b and c are integers. This is in turn equivalent to

    a != 0 ? b : c
    

    One way to achieve that is to find a way to turn non-zero value of a into an all-ones bit pattern using only bitwise operators. If we figure out how to do that, the rest would be easy. Now, I don’t immediately remember any ingenious tricks that would do that (they do exist I believe), and I don’t know for sure which operators are allowed and which are not, so for now I will just use something like

    a |= a >> 1; a |= a >> 2; a |= a >> 4; a |= a >> 8; a |= a >> 16;
    a |= a << 1; a |= a << 2; a |= a << 4; a |= a << 8; a |= a << 16;
    

    For a 32-bit integer type, if (and only if) there was at least one bit set in the original a, the above should result in all bits of a set to 1. (Let’s assume we are working with unsigned integers, to avoid the issues associated with shifting of signed values). Again, there must be a more clever way to do that, I’m sure. For example: a = !a - 1, but I don’t know if ! and - are allowed.

    Once we’ve done that, the original conditional operator becomes equivalent to

    (a & b) | (~a & c)
    

    Done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just have a quick question about the conditional operator. Still a budding programmer
I want to implement ternary conditional operator in MySQL. I have a table in
I have used the conditional operator in many places within my Java application. Now
Why can't the conditional operator be used as a statement? I would like to
HI, I need to write multiple statements inside a conditional operator. What will be
Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation
C and many other languages have a conditional (AKA ternary) operator. This allows you
Is it not allowed to have a conditional operator in a lambda expression in
I'm having some trouble with using the conditional operator to get a reference to
Why in conditional operator ( ?: ), second and third operands must have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.