Thread: Shifting Bits

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

    Shifting Bits

    If I use the code:
    #define RGB32BIT(a, r, g, b) ((b) + (g << 8) + (r << 16) + (a << 24)

    UINT pixel = RGB32BIT(0, 0, 255, 0);

    How do I get back the alpha, red, green, and blue values if I just have the pixel?
    Don't quote me on that... ...seriously

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    alpha = (pixel >> 24) & 0xff;
    red = (pixel >> 16) & 0xff;
    green = (pixel >> 8) & 0xff;
    blue = pixel & 0xff;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Congratulations! You have proven yourself to be among the ranks of the programmers that know too much. I knew the answer. I just wanted to see if anyone had a clue about it. I guess you do. I prefer:

    Code:
    UCHAR alpha = pixel >> 24;
    UCHAR red = pixel >> 16;
    UCHAR green = pixel >> 8;
    UCHAR blue = pixel;
    The
    Code:
    UCHAR
    automaticaly drops off the extra so you don't need the
    Code:
    & 0xFF
    . I put that up at the game programmers message board and I got all kinds of junk.
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about:
    Code:
    b = pixel & 0x000000ff;
    g = (pixel >> 8) & 0x000000ff;
    r = (pixel >> 16) & 0x000000ff;
    a = pixel >> 24;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Brad0407
    Congratulations! You have proven yourself to be among the ranks of the programmers that know too much. I knew the answer. I just wanted to see if anyone had a clue about it. I guess you do. I prefer:

    Code:
    UCHAR alpha = pixel >> 24;
    UCHAR red = pixel >> 16;
    UCHAR green = pixel >> 8;
    UCHAR blue = pixel;
    The
    Code:
    UCHAR
    automaticaly drops off the extra so you don't need the
    Code:
    & 0xFF
    . I put that up at the game programmers message board and I got all kinds of junk.
    But in your original code a, g, b, and r aren't UCHAR's. So now you're changing their original types.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Asked and answered in your previous post (which you claim you answered)
    http://cboard.cprogramming.com/showthread.php?t=65502
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Brad0407
    Congratulations! You have proven yourself to be among the ranks of the programmers that know too much.
    You're a genious..

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about something like:
    Code:
    union pixel_data
    {
      uint32_t pixel;
      struct
      {
        uint8_t alpha;
        uint8_t red;
        uint8_t green;
        uint8_t blue;
      } part;
    };
    Code:
    {
      union pixel_data pix1;
    
      pix1.pixel = 0x12345678;
    
      printf("red: %d\n", pix1.part.red);
      printf("green: %d\n", pix1.part.green);
      printf("blue: %d\n", pix1.part.blue);
    }
    Maybe? Not sure if word-alignment or padding would screw it up.
    If you understand what you're doing, you're not learning anything.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    enum { cAlpha, cRed, cGreen, cBlue };
    
    union foo
    {
        uint32_t pixel;
        uint8_t part[ sizeof( uint32_t ) ];
    };
    
    union foo bar;
    
    printf("Alpha: %d\n", bar.part[ cAlpha ] );
    Or an array. Unions are definately fun.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by itsme86
    Maybe? Not sure if word-alignment or padding would screw it up.
    That depends on the order of the variables in the definition, and correct usage of #pragma pack(...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM