shifts and masks

D

Dave

Hello -
Say I have a 32 bit value in data (v:4, r:4, l:8, p:16)

[ v | r | l | p ]
unsigned char data;

Now, for portabilities sake, I want to use shifts and masks to access
these fields, instead of accessing the bitfields directly.

Lets assume v has a value of 2. To view the value of v in data, I need
to right shift 28 bits (correct?). How do I figure out what the MASK
should be?

#define MASK 0xff

if ((data & MASK) >> 28 == 2) {
/* matches */
}

I am having trouble understanding how to come up with masks. What would
the masks be for r, l, and p?

Thanks
Dave
 
C

Coos Haak

Op Sat, 03 Jun 2006 08:16:54 -0400 schreef Dave:
Hello -
Say I have a 32 bit value in data (v:4, r:4, l:8, p:16)

[ v | r | l | p ]
unsigned char data;

Now, for portabilities sake, I want to use shifts and masks to access
these fields, instead of accessing the bitfields directly.

Lets assume v has a value of 2. To view the value of v in data, I need
to right shift 28 bits (correct?). How do I figure out what the MASK
should be?

#define MASK 0xff

if ((data & MASK) >> 28 == 2) {
/* matches */
}
Why not first shift and then mask the bits:

#define MASK 0x0F

if (((data >> 28) & MASK) == 2 ) {
/* matches */
}
I am having trouble understanding how to come up with masks. What would
the masks be for r, l, and p?

15, 255, 63
 
D

Dave

Coos said:
Op Sat, 03 Jun 2006 08:16:54 -0400 schreef Dave:
Hello -
Say I have a 32 bit value in data (v:4, r:4, l:8, p:16)

[ v | r | l | p ]
unsigned char data;

Now, for portabilities sake, I want to use shifts and masks to access
these fields, instead of accessing the bitfields directly.

Lets assume v has a value of 2. To view the value of v in data, I need
to right shift 28 bits (correct?). How do I figure out what the MASK
should be?

#define MASK 0xff

if ((data & MASK) >> 28 == 2) {
/* matches */
}
Why not first shift and then mask the bits:

#define MASK 0x0F

if (((data >> 28) & MASK) == 2 ) {
/* matches */
}
I am having trouble understanding how to come up with masks. What would
the masks be for r, l, and p?

15, 255, 63

How did you come up with the masks?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Coos said:
Op Sat, 03 Jun 2006 08:16:54 -0400 schreef Dave:
Hello -
Say I have a 32 bit value in data (v:4, r:4, l:8, p:16)

[ v | r | l | p ]
unsigned char data;

Now, for portabilities sake, I want to use shifts and masks to access
these fields, instead of accessing the bitfields directly.

Lets assume v has a value of 2. To view the value of v in data, I need
to right shift 28 bits (correct?). How do I figure out what the MASK
should be?

#define MASK 0xff

if ((data & MASK) >> 28 == 2) {
/* matches */
}
Why not first shift and then mask the bits:

#define MASK 0x0F

if (((data >> 28) & MASK) == 2 ) {
/* matches */
}
I am having trouble understanding how to come up with masks. What would
the masks be for r, l, and p?

15, 255, 63

How did you come up with the masks?

If you do the shifting first (as Coos Haak said) then the mask would be
the size of the value you want to access, in the case of v 4 bits, which
would make it 0x0f, the same for r. For l 8 bits (0xff) and 16 for p
(0xffff). To get the value of r shift 24, 16 for l and 0 for p.

If not the masks would be 0xf0000000, 0x0f000000, 0x00ff0000 and
0x0000ffff for v, r, l and p respectively, each hex digit represents 4
bits, so for v the first 4 bits are masked and not the rest and so on.

Erik Wikström
 
M

Malcolm

Dave said:
Say I have a 32 bit value in data (v:4, r:4, l:8, p:16)

[ v | r | l | p ]
unsigned char data;

Now, for portabilities sake, I want to use shifts and masks to access
these fields, instead of accessing the bitfields directly.

Lets assume v has a value of 2. To view the value of v in data, I need to
right shift 28 bits (correct?). How do I figure out what the MASK should
be?

#define MASK 0xff

if ((data & MASK) >> 28 == 2) {
/* matches */
}

I am having trouble understanding how to come up with masks. What would
the masks be for r, l, and p?
You need to write the mask out in binary

eg l would be

0000 0000 0000 0000 1111 1111 0000 0000

I've grouped them in fours because, by the magic of numbers, each four
binary digits can be repalced by one hexadecimal digit

0000 0000 0000 0000 1111 1111 0000 0000
0 0 0 0 F F 0 0

So your mask is 0x0000FF00

when we AND with the mask, all the digits in the range we are interested in
are preserved, because they are ANDed with one. All the digits we are not
interested in are set to zero.

Now to extract the number, we need it in the low bits of our integer. So
shift right by eight bits.

The whole expression becomes

p = (data & 0x0000FF00) >> 8;
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top