splitting 16bit to 2 x 8bit

Started by
10 comments, last by Merlz 17 years, 3 months ago
how would i split a 16bit value to two 8 bit values which i can store and than reconvert those 2 8bit values into the 16bit value...
Advertisement
in VC++ you can use HIBYTE or LOBYTE to extract the values (or use bit masks to construct equivalent macros). you can also just store the two values independently as char's.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
i need a pure mathematical way since im doing the converts in a shader
Bit mask aren't a mathematical way...?
The only real way to do it mathematically is to use ints, mod, and divide.

High byte: value/256
Low byte: value%256

As long as you're using integer values, that should do what you want.
yes it is....

so would it be like this then?

unsigned short _16bit;unsigned char _8bitLOW, _8bitHIGH;_8bitLOW= _16bit | pow(2,8);_8bitHIGH = _16bit | (pow(2,16)-pow(2,8));
Quote:Original post by Merlz
The only real way to do it mathematically is to use ints, mod, and divide.

High byte: value/256
Low byte: value%256

As long as you're using integer values, that should do what you want.


didnt see that.. ill try it out

EDIT:: how do i get back to the original value?
union Value {
unsigned short _16bit;
unsigned char _8bit[2];
};

With this you can access the 16 bit value or the two 8 bit values when ever you want without converting or reconverting anything =)
Quote:Original post by Dragon_Strike
how do i get back to the original value?

To get the original 16-bit value from two 8 bit values, you would do this:
(highByte*256)+lowByte

You are unbelivable.

byte A = (byte) (TheShort >>> 8);
byte B = (byte) (TheShort & 0xFF);

short TheShort = (A << 8) | B;

Now lets look at a paper about pixel shaders.
http://64.233.183.104/search?q=cache:2rpIwdgSvKYJ:graphics.cs.uiuc.edu/~jch/papers/pixelnoise.pdf+pixel+shader+bit+shifting&hl=en&ct=clnk&cd=3

It looks like it describes all what is necessary for the above in a pixel shader.

This topic is closed to new replies.

Advertisement