Jump to content
  • Advertisement

Archived

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

DaA

Slice a byte.

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
example:

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?

-Michael

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!