Masking

M

Mike Copeland

How do I mask a portion of an int that's been constructed from several
data fields? That is, I have an int value that contains several values;
bits 10-15 have something I want to isolate and use. I know I can bit-
shift back and forth, but is there a way to mask out these bits and use
them as an integer value? TIA
 
L

Leor Zolman

How do I mask a portion of an int that's been constructed from several
data fields? That is, I have an int value that contains several values;
bits 10-15 have something I want to isolate and use. I know I can bit-
shift back and forth, but is there a way to mask out these bits and use
them as an integer value? TIA

Use the binary & (bitwise and) operator:

unsigned int bits = 0xabcd;
unsigned int something = (bits >> 10) & 0x3f; // get bits 10-15

(The above assumes the low-order bit is called "bit 0")
-leor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top