Thread: Bit manipulation

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    Bit manipulation

    Hi, my question is to do with writing individual bits. I want to be able to write individual bits rather than a whole char or int. If I allocated myself a byte of memory how can I then write the individual bits myself, is this even possible? I want to be able to choose the 1/0 status myself and write this accordingly.

    EG:
    say I wanted to write 10010111 into a byte of memory how would I do this?

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is this even possible?
    Of course, C offers several bitwie operators that can be used in a cryptic and arcane manner to do exactly what you want. Not really, it just seems complicated at first.

    >say I wanted to write 10010111 into a byte of memory how would I do this?
    Here is one way:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int i = 0;
      int mask[] = { 1, 2, 4, 16, 128, -1 };
      unsigned char bits = 0;
    
      while ( mask[i] != -1 ) {
        bits |= mask[i]; /* Set the bit */
        ++i;
      }
    
      printf ( "%d\n", bits );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    I wrote an FAQ entry about this, but L@D has yet to put it up...*grumbles* lemme try and find a transcript.

    ahh...here we go!

    Code:
    Bit Manipulation/Bit Shifting - Basic overview
    ================================================
    
    Bit Manipulation/Shifting is the process of taking data, and toying with
    the binary of it (the basic 1s and 0s that it's comprised of).
    
    Bit operations in C/C++ are carried out with the following operators:
    
           -----------------------------------------------------------
           |Operator          Definition                                               |
           -----------------------------------------------------------|
           |  &        |    Bitwise AND (Not to be confused with &&)|
           |  |         |   Bitwise OR (Not to be conused with ||)      |
           |  ^        |    XOR (Exclusive OR)                                     |
           |  <<     |    Shift left                                                      |
           |  >>     |    Shift right                                                    |
           |  ~        |    Unary (One's compliment)                          |
           ------------------------------------------------------------
    
    
    OR is fairly simple: 
    The OR operator (|) compares two bits and returns true (1) if either or both bits are set to true. 
    
    code:--------------------------------------------------------------------------------
    (1 | 2)
    
       00000001
    |  00000010
    -----------------
       00000011
    --------------------------------------------------------------------------------
    
    
    It basically just adds the binary of the numbers. It doesn't actually add the numbers though, remember that. 
    
    AND is just as easy: 
    The AND operator (&) compares two bits and returns true (1) if both bits are set to true. 
    
    code:--------------------------------------------------------------------------------
    (1 & 3)
    
       00000001
    &  00000011
    ------------------
       00000001
    --------------------------------------------------------------------------------
    
    
    See what it did? everywhere there was a 1 in the first one and the second, there was a 1 in the resulting number. 
    
    XOR is trickier: 
    The XOR operator (^) compares two bits and returns true (1)  if only one of the bits are set to true. 
    
    code:--------------------------------------------------------------------------------
    (1 ^ 3)
    
       00000001
    ^  00000011
    ------------------
       00000010
    --------------------------------------------------------------------------------
    
    
    
    Anywhere there was a 1 in both numbers, it became a zero. 
    
    UNARY is the easiest. It basically means binary opposite: 
    
    
    code:--------------------------------------------------------------------------------
    (~1)
    
    ~0000000001 = 1111111110
    --------------------------------------------------------------------------------
    
    
    << and >> (shift left and right) are incredibly simple, and used the most: 
    
    000000010 >> 1 = 000000001 
    000000001 << 1 = 000000010 
    
    It basicall goes like this: number >> Amount to shift by 
    It moves the numbers down by the number you place to the right. 
    
    >> Is actually the quickest way to divide by 2 
    << Is actually the quickest way to multiply by 2 
    
    Another thing about shifts, is that the numbers don't carry! soo: 
    
    000000001 >> 1 = 00000000, NOT 10000000 
    
    
    And remember I wrote the numbers out in binary instead of the actual numbers you'd put in there. ( 000000011 is 3, 0000000010 is 2, and 000000001 is 1 binary) So remember, you don't write the numbers in binary, you write them in decimal to use the bit operators.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How about bitfields?

    Code:
    typedef unsigned char byte;
    struct bitfield{
      byte bit0 : 1;
      byte bit1 : 1;
      byte bit2 : 1;
      byte bit3 : 1;
      byte bit4 : 1;
      byte bit5 : 1;
      byte bit6 : 1;
      byte bit7 : 1;
    };
    There are a few drawbacks to bitfields however it does make it possible for you to do this
    Code:
    struct bitfield bf;
    
    bf.bit0 = 1;
    bf.bit1 = 1;
    bf.bit2 = 1;
    bf.bit3 = 0;
    bf.bit4 = 1;
    bf.bit5 = 0;
    bf.bit6 = 0;
    bf.bit7 = 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM