Hey!
So i have started learning c, quite refreshing when i normally code in Java :)
I have this task where i need to store three int values in an unsinged int via bitwise operators. Its for a RGB picture where the colour is limited to 256. I have done this succesfully(i hope :) ) :

unsigned int make_pixel(int red, int green, int blue) {
   unsigned int colour = 1;
   colour = colour << 9; //makes room for a 256bit colour
   printf("%u\n", colour);
   colour = red | colour; printf("red %u\n", colour);
   colour = colour << 8; //makes room for a 256bit colour
   colour = colour | green; printf("green %u\n", colour); 
   colour = colour << 8; //makes room for a 256bit colour
   colour = colour | blue; printf("blue %u\n", colour);

   return  colour;
}

Now i have also retrived the first value (the red one,):

int get_red(unsigned int p) {
   return (p>>16)-512;
}

My problem is that i do not know where to start on the middle and last values. My first thought was that you could 'push' the last value out with colour>>8 and catch it. But it doesn't work like that apperently :)

I have tried to make a chart of how the unsigned int should look at the end, if its wrong than my understanding of bitwise operators is wrong aswell :P :

1:      1
9<-:    100 000 000
red ->: 1rr rrr rrr
8<-:    1rr rrr rrr 000 000 00
green:  1rr rrr rrr ggg ggg gg
8<-:    1rr rrr rrr ggg ggg gg0 0 000 000
blue->: 1rr rrr rrr ggg ggg ggb b bbb bbb

Edited 4 Years Ago by Krokcy

Mask just the low byte after shifting and you'll get what you want:

int red = (p >> 16) & 0xff;
int green = (p >> 8) & 0xff;
int blue = p & 0xff;

Coincidentally, I did this very thing today while writing an image skew detection algorithm.

Edited 4 Years Ago by deceptikon

FYI, it would be easier to tell if the value was loaded properly if you print using HEX (%X) instead of integer (%u). Food for thought.

I have tried to make a chart of how the unsigned int should look at the end, if its wrong than my understanding of bitwise operators is wrong aswell :P :

Print in both Hex and Decimal to compare, see if there is any truth to your assumption/understanding.

Awsome that did the trick :)
However, i cant quite get my head around what exactly you are doing.

When i put the values into the unsigned int:

colour = red | colour; printf("red %u\n", colour);

It takes the longer value and puts into colour. But with the operator & it takes the shorter value. Like this (the values are just for show..):

colour = 00101000100011001000
red    = 10010110
colour = colour << 8;

*when this happens it moved the bits 8bits to the left so:

colour= 0010100010001100100000000000

colour = colour|red

/*than it compares the two so:
   0010100010001100100000000000
                       10010110 |
=  0010100010001100100010010110 
*/

red = colour & 0xff

/*than to get the last bits:
      0010100010001100100010010110
                          11111111 &
                   red  = 10010110  


  **I dont understand why red doenst get the long value, 
  when with the operator | colour did ..

Its hard to explain since im entirely sure i understand it correctly...

Edited 4 Years Ago by Krokcy

>/*than it compares the two so:
>   0010100010001100100000000000
>                       10010110 |
>=  0010100010001100100010010110 
>*/

No.... Look up what a single | does.

>red = colour & 0xff
>
>/*than to get the last bits:
>      0010100010001100100010010110
>                          11111111 &
>                   red  = 10010110  

And a single &

And then word is then, not than ;o)

This question has already been answered. Start a new discussion instead.