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

  • SEARCH
  • Home
  • 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 7820141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:10:56+00:00 2026-06-02T07:10:56+00:00

I am having trouble with the last problem of my bit twiddling homework exercise.

  • 0

I am having trouble with the last problem of my bit twiddling homework exercise. The function is supposed to return 1 if any odd bit is set to 1.
Here is what I have so far:

int anyOddBit(int x) {
    return (x & 0xaaaaaaaa) != 0;
}

That works perfectly, but I am not allowed to use a constant that large (only allowed 0 through 255, 0xFF). I am also not allowed to use !=

Specifically, this is what I am limited to using:

  Each "Expr" is an expression using ONLY the following:
  1. Integer constants 0 through 255 (0xFF), inclusive. You are
      not allowed to use big constants such as 0xffffffff.
  2. Function arguments and local variables (no global variables).
  3. Unary integer operations ! ~
  4. Binary integer operations & ^ | + << >>

I can’t figure out how to do this within in those restrictions and I’d really appreciate it if someone could point me in the right direction. Thanks in advance!

  • 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-06-02T07:10:58+00:00Added an answer on June 2, 2026 at 7:10 am

    You can use:

    !!((   ( x        & 0xff)
         | ((x >>  8) & 0xff)
         | ((x >> 16) & 0xff)
         | ((x >> 24) & 0xff)
    ) & 0xaa)
    

    The “inner” bit, which ORs together each source octet, will give you an octet where each bit is set if the equivalent bit is set in any of the source octets. So, if one of the odd bits is set in the source octets, it will also be set in the target one.

    Then, by simply ANDing that with 0xaa, you get a zero value if no odd bits are set, or a non-zero value if any of the odd bits are set.

    Then, since you need 0 or 1, and you can’t use !=, you can acheive a similar effect with !!, two logical not operators. It works because !(any-non-zero-value) gives 0 and !0 gives 1.


    In order to do it with 12 operators only (rather than 13 as per my original solution above), you can remove the & 0xff for the >> 24 value since it’s not actually necessary (zero-bits are shifted in from the left):

    !!((   ( x        & 0xff)
         | ((x >>  8) & 0xff)
         | ((x >> 16) & 0xff)
         | ((x >> 24)       )
    ) & 0xaa)
    

    In fact, you can do even better than that. The final & 0xaa will clear out all the upper 24 bits anyway so no & 0xff sections are needed (it also fits on one line as well):

    !!((x | (x >> 8) | (x >> 16) | (x >> 24)) & 0xaa)
    

    That gets it down to nine operators.

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

Sidebar

Related Questions

I’m having trouble retrieving the YouTube video automatically. Here’s the code. The problem is
I am having a little bit of a trouble with the following: <script> $(document).ready(function()
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
Having trouble with each function... Will try to explain by example... In my code,
I just started learning Java and I'm having trouble formatting string. In the problem
I’m having a bit of trouble controlling the results from a data generating algorithm
I'm having trouble figuring out my last section of code for a Dynamic Coin
I am having trouble getting PYTHONPATH set. I have added this line to ~/.bashrc
hay for the last week im having real trouble with my site. i hed
I'm having trouble to implement a simple list in C, the problem is 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.