473,480 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

shifts and masks

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
Jun 3 '06 #1
4 4948
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
--
Coos
Jun 3 '06 #2
Coos Haak wrote:
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?
Jun 3 '06 #3
On 2006-06-03 14:47, Dave wrote:
Coos Haak wrote:
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
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Jun 3 '06 #4
"Dave" <da***@wmol.com> wrote
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;

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1687
by: Glen Able | last post by:
The behaviour of << and >> arithmetic operators (under VC++6, x86) seems rather odd to me. Shifts at runtime seem to only use the bottom 5 bits of the shift amount (an x86 issue I suppose), but...
0
1299
by: Gianni Mariani | last post by:
Below is an example of how to do static type-checking of an erroneous set of input masks. The drawback is that it's a little too verbose and non-trivial to understand and to make it truly I'd...
19
4216
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
3
2412
by: Dave | last post by:
I need to rewrite the following using shifts and masks for portability sakes, and to get rid of the BYTE_ORDER issue. /* CODE */ struct test { #if BYTE_ORDER == BIG_ENDIAN unsigned char ...
10
2165
by: noridotjabi | last post by:
I have often times seen masks used in C source code. I really don't understand the purpose or functionality of this. I have seen used: #define MASK 0x77 .... if(hello & MASK) Also I...
3
2026
by: larzeb2000 | last post by:
I have constructed a css stylesheet which has a centered layout. The wrapper div is defined as 1000px. It contains a header div for the entire wrapper width, and a left sidebar div and a content...
15
1758
by: Christopher Layne | last post by:
So I recently ran into a situation where I invoked UB without specifically knowing I did it. Yes, human, I know. What exactly is/was the rationale for not allowing shifts to be the same width of...
2
2167
by: skijor | last post by:
For a page that display's a catalogue of items in table format. If the number of rows extends below the view, in some browsers (safari, firefox) the page shifts to the left a little bit. It's...
10
1756
by: dave | last post by:
Hello, I made a function that takes a word list (one word per line, text file) and searches for all the words in the list that are 'shifts' of eachother. 'abc' shifted 1 is 'bcd' Please take...
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.