468,504 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

C - Convert RGB(31,31,31) to 16 bit colour.

1. Write a function or macro, which takes three values between 0 and 31, and converts this to BGR 16 bit format. For example, the function prototype should look like u16 BGR(u16 b, u16 g, u16 r);
That's the assigment from my teacher, but i think in 'normal' terms that means:

Write a function that takes RGB(r,g,b); r, g and b being values between 0 and 31; it then converts it to 16 bit RGB.

I haven't a clue how to do that, or what it means.

Thanks in advance.
Nov 18 '07 #1
16 3916
JosAH
11,448 Expert 8TB
That's the assigment from my teacher, but i think in 'normal' terms that means:

Write a function that takes RGB(r,g,b); r, g and b being values between 0 and 31; it then converts it to 16 bit RGB.

I haven't a clue how to do that, or what it means.

Thanks in advance.
Note that a value in the range [0, 31] takes up 5 bits. Three of them next to each
other take up 15 bits. That fits nicely in a 16 bit word. Have a look at the bit shift
operators << and >>

kind regards,

Jos
Nov 18 '07 #2
Note that a value in the range [0, 31] takes up 5 bits. Three of them next to each
other take up 15 bits. That fits nicely in a 16 bit word. Have a look at the bit shift
operators << and >>

kind regards,

Jos
That doesn't really mean much to me. I haven't done much with bits, bit shift etc..
Nov 18 '07 #3
JosAH
11,448 Expert 8TB
That doesn't really mean much to me. I haven't done much with bits, bit shift etc..
Well: start reading then.

kind regards,

Jos
Nov 18 '07 #4
Well: start reading then.

kind regards,

Jos
It would help if you could sort of lead me in the right direction.
Nov 18 '07 #5
JosAH
11,448 Expert 8TB
It would help if you could sort of lead me in the right direction.
That was exactly what I was doing: you have to solve the problem how to fit
three 5 bit numbers in one 16 bit number (there'll be one bit left then).

kind regards,

Jos
Nov 18 '07 #6
That was exactly what I was doing: you have to solve the problem how to fit
three 5 bit numbers in one 16 bit number (there'll be one bit left then).

kind regards,

Jos
Thanks, but i know what i have to do. The question was and still is, how do i do it?
Nov 18 '07 #7
JosAH
11,448 Expert 8TB
Thanks, but i know what i have to do. The question was and still is, how do i do it?
Well I gave you a hint already: read about the bit shift operators << and >>
but you simply said you know nothing about them, on which I replied that you
should read up on them. You don't want to keep this going on ad nauseam do you?

kind regards,

Jos
Nov 18 '07 #8
Well I gave you a hint already: read about the bit shift operators << and >>
but you simply said you know nothing about them, on which I replied that you
should read up on them. You don't want to keep this going on ad nauseam do you?

kind regards,

Jos
So basically:

Shifting left using << causes 0's to be shifted from the least significant end (the right side), and causes bits to fall off from the most significant end (the left side). This occurs whether the variable is signed or unsigned.
Shifting left by K bits is equivalent to multiplying by 2^K.
Shifting right using >> causes 0's to be shifted from the most significant end (the left side), and causes bits to fall off from the least significant end (the right side) if the number is unsigned. If it's signed, then it's implementation depedendent. It may shift in the sign bit from the left, or it may shift in 0's (it makes more sense to shift in the sign bit).
For unsigned int, and sometimes for signed int, shifting right by K bits is equivalent to dividing by 2^K (using integer division).
Bitshifting doesn't change the value of the variable being shifted. Instead, a temporary value is created with the bitshifted result.
So basically i have to change 31 into a 5 bit number? This is really confusing me.
Nov 18 '07 #9
AHMEDYO
112 100+
HI...

look 31 is basically 5 bits in memory, you was defined R,G, and B as integer thats mean each one holds in 16 bits,you must take only 5 bits fom each 16 and then merge '3 5bits in one integer variable

GOOD LUCK
Nov 18 '07 #10
JosAH
11,448 Expert 8TB
So basically:



So basically i have to change 31 into a 5 bit number? This is really confusing me.
You do realize that any value in the range [0,31] takes up only 5 bits do you?

kind regards,

Jos
Nov 18 '07 #11
oler1s
671 Expert 512MB
You should let us know if you are having trouble understanding the fundamental concept of numbers and their representation as bits. If the idea that a number from 0-31 takes only 5 bits is not instantaneously intuitive, then ask us why.

The syntax for this problem is very easy. You only need to know about bit shifts. The concepts are also easy, but if you don't understand the concept, you can't write the few lines of code.
Nov 18 '07 #12
JosAH
11,448 Expert 8TB
How a simple problem can become a complicated educational issue. If only the OP
would come back so this entire little thingy could be solved in a nice way.

kind regards,

Jos
Nov 18 '07 #13
How a simple problem can become a complicated educational issue. If only the OP
would come back so this entire little thingy could be solved in a nice way.

kind regards,

Jos
I don't understand anything about bits really.

Why can't you just post the solution, with annotations so i understand what it does? Then we'd all be happy.
Nov 18 '07 #14
JosAH
11,448 Expert 8TB
I don't understand anything about bits really.

Why can't you just post the solution, with annotations so i understand what it does? Then we'd all be happy.
No we won't:

1) you still won't understand;
2) if you'd turn our work in you'd be cheating;
3) we don't cooperate with cheating;
4) future coworkers won't like you as a cheater;
5) at the end you won't be happy either.

Read up on those simple bit shifting operators and don't try to cheat.

kind regards,

Jos
Nov 18 '07 #15
No we won't:

1) you still won't understand;
2) if you'd turn our work in you'd be cheating;
3) we don't cooperate with cheating;
4) future coworkers won't like you as a cheater;
5) at the end you won't be happy either.

Read up on those simple bit shifting operators and don't try to cheat.

kind regards,

Jos
I have read up on them, but they don't seem so simple. Maybe you could explain them, if you don't think i'll understand...
All you have to do is explain to me this bit shifting and bits in general. Then how i would use it for the concept of RGB.

But no.
Nov 18 '07 #16
oler1s
671 Expert 512MB
I have read up on them, but they don't seem so simple. Maybe you could explain them, if you don't think i'll understand...
But if you don't understand the material you find in your book or with Google, then you won't understand our explanations either. If there's a concept you don't understand, something about the material you don't get, you need to tell us what it is you don't understand.

All you have to do is explain to me this bit shifting and bits in general. Then how i would use it for the concept of RGB.
The concept can be explained with a Google search. You complain you don't understand the material. How do we know our explanation won't be wasted on you?

Moreoever, figuring out how to use bit shifting for RGB is the homework assignment. You need to think about it. As far as we can tell, you just want the answer, and you'll take the explanation if you feel like it.

I'll give you a short description of bits and bit shifting. Let's see if you go anywhere with this.

A bit can be thought of as an on/off switch, because that's effectively what it is in hardware. Let's call off 0 and on 1. These would be your binary digits. When it comes to numbers, bits can correspond perfectly with a binary number. Let's say you have two bits. You can have this.
Expand|Select|Wrap|Line Numbers
  1. Bit 1    Bit 0        Binary Number        Decimal Number
  2.     0        0                        00            0
  3.     0        1                        01            1
  4.     1        0                        10            2
  5.     1        1                        11            3
  6.  
Can you see a pattern with how many bits you need to have how large a number? R, G, and B go from 0 to 31. How many bits do they need minimum?

Now consider bit shift. As it sounds, it's simply a shift:
0010 << 1 becomes 0100
0001 << 3 becomes 1000
It's the reverse direction for >>.

Now think. Knowing how many bits there are in R, G, and B, and using bit shifting, how can you form a 16 bit "number" from R,G, and B?
Nov 18 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Muffinman | last post: by
2 posts views Thread by Adam Teasdale Hartshorne | last post: by
4 posts views Thread by Steven | last post: by
1 post views Thread by Anonieko Ramos | last post: by
5 posts views Thread by JJ | last post: by
5 posts views Thread by beertje | last post: by
4 posts views Thread by Arun | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.