Bit access

This is a discussion on Bit access within the C++ Programming forums, part of the General Programming Boards category; Does anyone know an easy way to access individual the bits of, say, an unsigned int. I know there are ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Bit access

    Does anyone know an easy way to access individual the bits of, say, an unsigned int. I know there are methods that involve shifting the number and using bit-wise operators to determine the bit value. I would like to perform quick and repeatable access to the bits and don't want to destroy the data in the process of reading it. I assume there is some easy command using pointers.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    I prefer a bit more of an elegant solution

    Code:
    union ByteBitfield
    {
        unsigned char Value;
        struct
        {
            bool Zero : 1,
                    One : 1,
                    Two : 1,
                    Three : 1,
                    Four : 1,
                    Five : 1,
                    Six : 1,
                    Seven : 1;
        } Bits;
    };
    Now you can access the bits and the value quickly and easily without the nasty shifting anding and oring that's in Salem's example!

    Code:
    ByteBitfield MyData;
    
    MyData.Value; // Gets you the value of the number
    
    MyData.Bits.Zero; // Gives you "true" if the Zero bit is set to 1
    
    MyData.Bits.One; // Gives you "true" if the One bit is set to 1
    
    if( MyData.Bits.Three )
    {
        /* Do Something */
    }
    
    // You can also change the bit extremely easily
    
    
    MyData.Bits.Three = true; // Set's bit Three to 1
    
    MyData.Bits.Four = !MyData.Bits.Four; // Toggles bit Four
    See! No nasty shifting, compilments, anding oring, or even comparisons!

    Sorry to disagree with you Salem!

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Polymorphic OOP
    I prefer a bit more of an elegant solution
    I prefer an even more elegant solution

    Code:
    #include <bitset>
    #inlude <iostream>
    int main()
    {
       std::bitset<32> b = 0;
        b[2] = 1;
        b[5] = 1;
        
        std::cout << b.to_ulong();
    }
    Use the standare headers as much as possible.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Sang-drax
    Use the standare headers as much as possible.
    The standard libraries take all the fun out of coding. They should die of gohnnoreah and rot in hell.

    ...yes, i know i spelled that horribly, horribly wrong

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Using the bitset class would work, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any faster way to access bit maps?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2005, 07:56 PM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  4. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM
  5. Bit manipulation
    By pkalluri in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 07:06 AM

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