# splitting 16bit to 2 x 8bit

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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...

##### Share on other sites
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.

##### Share on other sites
i need a pure mathematical way since im doing the converts in a shader

##### Share on other sites
Bit mask aren't a mathematical way...?

##### Share on other sites
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.

##### Share on other sites
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));

##### Share on other sites
Quote:
 Original post by MerlzThe only real way to do it mathematically is to use ints, mod, and divide.High byte: value/256Low byte: value%256As 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?

##### Share on other sites
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 =)

##### Share on other sites
Quote:
 Original post by Dragon_Strikehow 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

##### Share on other sites
You are unbelivable.

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

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

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

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633329
• Total Posts
3011382
• ### Who's Online (See full list)

There are no registered users currently online

×