1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Setting the even bits to 1

  1. Sep 28, 2008 #1
    1. The problem statement, all variables and given/known data
    /*
    * evenBits - return word with all even-numbered bits set to 1
    * Legal ops: ! ~ & ^ | + << >>
    * Max ops: 8
    * Rating: 2
    */
    int evenBits(void) {
    return ?;
    }

    3. The attempt at a solution

    I have no idea! What I originally thought was that I would have to perform a shift by 31 (whether left or right, I dont know) so that all the numbers would be set to all 0's or 1's and work from there, but then I realized that the odd bits should probably remain the same. I guess I could create an int var, of -1 or something and go from there, but really I just don't know if that would accomplish anything. Any help would be greatly appreciated.

    P.S does anyone know of some links that could give me a better understanding of all these bitwise and logical operations? Because I also have to do stuff like: if x <= y then return 1, else return 0 / implement the ! operator, using all of the legal operators except !/ x+y+z using only a single '+', and I would love to get a better understanding why/how to do this, instead of fumbling around for hours on end. Thanks!
     
  2. jcsd
  3. Sep 28, 2008 #2

    Borek

    User Avatar

    Staff: Mentor

    Both bytes - older and younger - will be identical. All you have to do then is to move one byte to the left and to combine it with the other one.
     
  4. Sep 28, 2008 #3
    Hrmm I think I see what you're saying, basically do a left shift by a byte (x << 4, correct?) and then or it with what the original was? But nothing is being passed in to the function, and I need a variable in order to do the left shift correct? I guess i'm still having trouble comprehending how exactly the shift is done with nothing passed. Thanks for the help!
     
  5. Sep 29, 2008 #4

    Borek

    User Avatar

    Staff: Mentor

    How many bits in byte?

    You don't need a variable, you can shift just a value. For example common way of combining characters into integers is

    code = ('R' << 24) + ('I' << 16) + ('F' << 8) + 'F';

    this is sometimes used to prepare constants that will be used in fast checking if the data starts with a given character sequence ("RIFF" in this case) - instead of doing string comparison you are doing much faster int comparison.

    In case you wonder: RIFF stands for Resource Interchange File Format and all AVI and WAV files start with this sequence.
     
  6. Sep 29, 2008 #5
    *smacks self on forehead* 8, there are 8 bits in a byte!

    So then I should probably have something like:

    ('A' << 8) | A correct?
     
  7. Sep 30, 2008 #6

    Borek

    User Avatar

    Staff: Mentor

    Seems OK. Just select correct value for A.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Setting the even bits to 1
  1. Parity Bit (Replies: 1)

Loading...