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