Thread: quick question about bit manipulation

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    quick question about bit manipulation

    I'm pretty new to bit manipulators and I have a quick question. Say I've got a byte set to something like "10110101" and I want to set the the 3rd bit to 0. I know I need to XAND it with the byte "11011111" but I'm not sure of the easiest way to do this in a program. I can't just write:
    Code:
    // x is equal to an arbitrary byte and I want to set the third bit to zero
    x &= 11011111;
    can I? Is there an easy way to write it simply like this or do I need to calculate out what the base-10 value of 11011111 is and instead XAND it with that? Basically I want to end up writing a function that will set a particular specified bit to zero.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Duh, didn't think about using the XOR operator, thats a much simpler process. Thanks, I think I will use macros, should be much easier.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I do it exactly like you, PJYelton. This makes your code very readable, although I usually use hex format which is simpler to type, but not as readable for those who don't know how to do hex to binary conversions in their heads.

    I hope I don't screw-up the logic here... But These are the things I do quite frequently:

    AND with ONE in test position(s) to test if a bit is one. (i.e. false if bit is zero, true if one.)

    AND with ZERO to force to zero

    OR with ONE to force to one

    I often use the invert operator '~' if I need "set" and "clear" the same bits repetedly. (i.e. if under one set of conditions I need to set bits 1 and 3 to zero, and later set bits 1 and 3 to one.)

    I don't think I've used XOR to "flip a bit" because I usually have to just read or write a particular bit, or I need to confirm it's state before inverting it.

    I've been burned by the size-of problem too! (But I'm usually working on non-portable hardware-specific code when I'm manipulating bits.) I think it would be acceptable to use use "size-of" to generate your mask.

    As far as I know a byte is always 8 bits... at least in my (hardware) world. There is no "byte" type in C++.

    Finally... bits are usually identified by counting left from the LSB (Least Significant Bit) starting with zero (kinda like arrays). So a byte contains bits 0 thu 7. What you're calling the "third" bit is "bit 5" (and it has a "weight" of 32). Sometimes we might call it "the 32 bit", but I don't think it's technically correct... maybe that's engineering slang.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I'm pretty new to bit manipulators and I have a quick question. Say I've got a byte set to something like "10110101" and I want to set the the 3rd bit to 0. I know I need to XAND it with the byte "11011111" but I'm not sure of the easiest way to do this in a program. I can't just write:
    This is actually bit 6 or bit 5 if you count by 0.
    I would recommend having a group
    of mask like unsigned char mask[5] = 0x00df. Then if you
    have to set a bit you would do x |= mask[5] and to clear
    a bit x &= ~mask[5]

    Bytes are not always 8 bits, there are a few machines that
    don't have char's having 8 bits like
    the Cray super computer. But because
    there are so few machines and bit manipulation is usually
    in level parts of the system I don't see anything wrong
    with assuming bytes are 8 bits. You should make sure
    that you use unsigned char. To do this
    just typedef unsigned char uint_8.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks for all your help guys! While I've always understood binary numbers I just started to work with bit manipulation in programming about 3 days ago, and I got hit by a landslide of information to learn!

  6. #6
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Re: quick question about bit manipulation

    Code:
    x&=223; //what you call the 3rd bit is the 5th bit.
    11011111 = 255-32 = 223

    thats how i do it. i dont know what vVv is talking about there. I dont think its that complicated.. or did i miss something.
    I AM WINNER!!!1!111oneoneomne

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Didn't say it was complicated, I was just wondering if there was another way to do it other than calculating out the base-10 value like you did.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    #include <climits>
    ...
    #define THIRD_BIT( x ) ( 1 << ( ( CHAR_BIT * sizeof( x ) ) - 3 ) )
    ...
    char x = 32;
    if( x & THIRD_BIT( x ) ) /* is third bit set? */
      x ^= THIRD_BIT( x ); /* yes, clear it */
    There is something wrong with this right?
    I think because it will do the subtraction and the shift using signed integer arithmetic. It probably won't work on all machines anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. Question about Bit Manipulation.
    By parastar in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 08:46 AM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  4. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM