Accessing a specific bit

This is a discussion on Accessing a specific bit within the C++ Programming forums, part of the General Programming Boards category; I want to know if there's a way I can access a specific bit in a byte. Example, a byte ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Accessing a specific bit

    I want to know if there's a way I can access a specific bit in a byte. Example, a byte looks like this: 00001001. What if I want to access 00001001, end test whether it's true or false.

    I've tried using bit-shifting to the left, with the value of the bit's spot from the end-1, in this case: << 3, which results in 01001000. and then shifting >> 7, which results in that the specific bit is the only one left.

    This doesn't work though, because the bits shifted away come back. The only explanation I can find to this is that they are passed over to the next byte. I tried this with int though, which is only one byte long, right?

    Anyway, can I make this method work, or do it in an easier way?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Awwwwww! How stupid of me! If an int would be a byte long, the maximum value would be 256! Anyway, the problem remains.

  3. #3
    Unregistered
    Guest
    bit masking

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Try using the shift as you did, but '&' the value with 1 afterwards

    IE

    00011001 >> 3 = 00000011

    Now mask it with 1

    00000011 & 00000001 = 00000001

    now if that == 1...the bit was set.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Or map the variable onto a bitfield with pointer conversion. Or just use a union with a bitfield in it.

    Code:
    typedef union
    {
        bitfield_t      my_bitfield;
        unsigned int    my_int;
    } bits;
    
    bits my_var;
    
    // Assign the value
    my_var.my_int = another_var;
    
    // Access a single bit
    my_bit = my_var.my_bitfield.bit_5;

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You could shift, and not even have to use a bit mask - if the resulting, shifted number is odd, then that bit was initally set...

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    You could also replace "& 1" with "| 0", right?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Bah... all this bit shifting and masking? Just mask the value using and, if the result is not zero then the bit was set, otherwise it was not set.
    Code:
    const int iBitOneMask =   0x00000001;
    const int iBitTwoMask =   0x00000002;
    const int iBitThreeMask = 0x00000004;
    ...
    ...
    ...
    const int iBitSevenMask = 0x00000040;
    const int iBitEightMask = 0x00000080;
    ...
    ...
    ...
    bool bBitWasSet;
    int iValue;
    ...
    ...
    ...
    // Test if bit three of iValue was set.
    if( iValue & iBitThreeMask ) bBitWasSet = true;
    else bBitWasSet = false;
    ...
    ...
    ...
    // Test if bit eight of iValue was set.
    if( iValue & iBitEightMask ) bBitWasSet = true;
    else bBitWasSet = false;
    Wouldn't that work? No bit shifting needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 01:00 PM
  2. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  3. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM
  5. Declaring int of specific bit size
    By Natase in forum C Programming
    Replies: 6
    Last Post: 08-26-2001, 10:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21