Thread: Bit Shifting And Masking

  1. #1
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341

    Bit Shifting And Masking

    I have two main questions:

    1. What does the '&' actually do to the number in binary? How do I convert the number back?

    Code:
    #define RGB16BIT565(r,g,b)  ((b & 31) + ((g & 63) << 5) + ((r & 31)<<11))
    For example, if I put in:
    Code:
    color = RGB16BIT565(255, 0, 0);
    what does the 255 change to?

    2. Is this the proper way to get the original value back?

    Code:
    #define RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24)
    
    color = RGB32BIT(0, 255, 0, 0);
    
    red = (color >> 16) & 255;
    Would this code successfully return the number 255?

    Please don't numb down your answers. I'm not stupid. I have just never been taught.
    Don't quote me on that... ...seriously

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Um, it might help if you explain exactly what you're trying to do. I am stupid, so you need to dumb down your question for me.
    My best code is written with the delete key.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The binary AND (&) operator takes 2 integers and compares each bit in both numbers. It's probably easier to see than read. Let's say you had 2 8-bit numbers and you AND them together, you'd get:

    11000100 (number 1)
    01100101 (number 2)
    -------------
    01000100 (result)

    If the bit at a given position in both numbers is 1, that bit position in the result will also be 1. Bitwise AND is useful for bit masking. Say you have an 8-bit number and you only want the lower 4 bits of it...you could do:

    10011101
    00001111
    -------------
    00001101

    By doing that, you make it impossible for the upper 4 bits to result in 1, and the lower 4 bits will result in whatever they were in the original number.

    So you can encode all 3 8-bit rgb values into a 32-bit int easily and still have 8-bits left over. Say you pass in 3 8-bit values that represent the rgb of a color:

    00111001 (red)
    01101010 (green)
    10100111 (blue)

    If you store blue in the lowest 8 bits of the 32-bit int, green in the next 8 bits, and red in the 8 bits just above that you can encode it by shifting the red color 16 bits to the left and the blue color 8 bits to the left, then bitwise ORing them all together. So you've got:

    00000000001110010000000000000000 (red << 16)
    00000000000000000110101000000000 (green << 8)
    00000000000000000000000010100111 (blue)

    ORing them all together you end up with:

    00000000001110010110101010100111 (color)

    ORing works like ANDing, but if either bit in the specific location is 1, the result is 1. To reverse it and break the color back into rgb values you'd use masking:

    red = (color >> 16) & 255;
    green = (color >> 8) & 255;
    blue = color & 255;

    So basically you're just shifting the 8 bits you want into the lowest 8 bits and then masking off everything else.

    Hopefully that all made sense.
    Last edited by itsme86; 10-18-2005 at 03:12 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Ok. Thanks.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying bit by bit
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 11-29-2005, 05:52 PM
  2. Hexadecimal and Binary
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-15-2002, 11:41 PM
  3. 16 bit colors
    By morbuz in forum Game Programming
    Replies: 13
    Last Post: 11-10-2001, 01:49 AM