This topic is now archived and is closed to further replies.


Slice a byte.

Recommended Posts

I´ve got a char (1 byte) or 8 bits, and want to use the last 4 bits but how can I delete the first 4 bits.

Share this post

Link to post
Share on other sites
What language are you using?
I assume c or c++.

to "delete" some bits from a byte, you have to mask them out.
and you can''t really "delete" them, they''re still there, I assume you just want to set them to 0.

You have 8 bits in a byte (as stated)
what you want to do is AND two bytes together.

source_byte = source_byte & mask_byte;

where source_byte is the... source byte
and mask_byte is the mask
by performing a bitwise AND you can blank out specific bits

if the mask were 11111111 (bits)
source_byte & mask_byte would = the original source_byte
(all 1s, no bits ANDed out)
if the mask were 11110000 (again, these are the bits)
source_byte & mask_byte would = xxxx0000 where x are the original bits from source_byte.

Any questions?


Share this post

Link to post
Share on other sites