473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

BITWISE SHIFT QUESTION?

ok I want to extract the first 4 bits of a 32 bit interger. The SIZE
doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit
0,1,2 and,3. I want to exract these and place into another variable.

here is my Idea

int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4

or

long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15

to extract bit field I should shift right then use a bit mask
containing all ones. This will store the first 4 bits into bits1to3
right?

I beleive this is correct! but anyone else have suggestions?

Apr 13 '06 #1
4 3088
"HARDCORECODER" <do*************@gmail.com> writes:
ok I want to extract the first 4 bits of a 32 bit interger. The SIZE
doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit
0,1,2 and,3. I want to exract these and place into another variable.
myvar & 0xf will do that, given your apparent definition of "first".
here is my Idea

int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4

or

long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15

to extract bit field I should shift right then use a bit mask
containing all ones. This will store the first 4 bits into bits1to3
right?


I don't see why you want to do a shift if you want the
least-significant 4 bits.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Apr 13 '06 #2
HARDCORECODER wrote:
ok I want to extract the first 4 bits of a 32 bit interger. The SIZE
doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit
0,1,2 and,3. I want to exract these and place into another variable.
The "first 4 bits" is an ambiguous phrase, as is "bit 0, 1, 2 and 3". From
which end do you wish to number them?
here is my Idea

int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4
Your first statement, variable name and comment all disagree on what you're
trying to do. As far as the statement goes you apparently want bits 1, 2, 3
and 4 (numbering the least significant bit 0) in positions 0, 1, 2 and 3.

If you instead want the four least significant bits, you can simply do x & 0x0f.
or

long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15
Similarly, you are extracting bits 12-15 here. If 'x' is a 16-bit unsigned
integer, those are the four most significant bits.
to extract bit field I should shift right then use a bit mask
containing all ones. This will store the first 4 bits into bits1to3
right?

Not quite. You may be off by one. The four least significant bits are 0-3,
not 1-4, and they do not need to be shifted.

S.
Apr 14 '06 #3
Ben Pfaff wrote:

"HARDCORECODER" <do*************@gmail.com> writes:
ok I want to extract the first 4 bits of a 32 bit interger. The SIZE
doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit
0,1,2 and,3. I want to exract these and place into another variable.


myvar & 0xf will do that, given your apparent definition of "first".
here is my Idea

int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4
That "works" is you have 5-bit ints.
or

long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15
And 16-bit longs.
to extract bit field I should shift right then use a bit mask
containing all ones. This will store the first 4 bits into bits1to3
right?


I don't see why you want to do a shift if you want the
least-significant 4 bits.


I don't think ke doesn't want the least-significant bits -- it sounds
more like he wants the most-significant bits. (At least, that's what
it appears he wants. Maybe. Perhaps.)

Or, maybe he's not sure what he wants.

How about something like:

( foo >> ( (sizeof(foo) * CHAR_BITS) - 4 ) ) & 0x0f

Am I correct that "sizeof(foo)*CHAR_BITS" gives you the number of bits
in "foo"?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 14 '06 #4


Kenneth Brody wrote On 04/14/06 12:58,:
Ben Pfaff wrote:
"HARDCORECODER" <do*************@gmail.com> writes:

ok I want to extract the first 4 bits of a 32 bit interger. The SIZE
doesn't really matter. 8,16, 0r 32. I want the first 4 bits.....bit
0,1,2 and,3. I want to exract these and place into another variable.


myvar & 0xf will do that, given your apparent definition of "first".

here is my Idea

int bits1to3=(x>>1)&0x0f; // bits 1 2 3 4

That "works" is you have 5-bit ints.

or

long bits1to3=(x>>12)&0x0f; // bits 12 13 14 15

And 16-bit longs.

to extract bit field I should shift right then use a bit mask
containing all ones. This will store the first 4 bits into bits1to3
right?


I don't see why you want to do a shift if you want the
least-significant 4 bits.

I don't think ke doesn't want the least-significant bits -- it sounds
more like he wants the most-significant bits. (At least, that's what
it appears he wants. Maybe. Perhaps.)

Or, maybe he's not sure what he wants.

How about something like:

( foo >> ( (sizeof(foo) * CHAR_BITS) - 4 ) ) & 0x0f

Am I correct that "sizeof(foo)*CHAR_BITS" gives you the number of bits
in "foo"?


It does, but there are two nagging difficulties.

- If `foo' has padding bits, bits that are part of its
representation but not part of its value, you'll be shifting
by too many places. This seems to be mostly a "theoretical"
concern, as C implementations where integer types have padding
bits are about as rare as snowballs in Hell. Still, the
Standard permits the peculiar practice, and if such a machine
is ever built Murphy's Law ensures that your code will try to
run on it.

- If `foo' is a signed type with a negative value, the
effect of right-shifting is undefined. It may or may not
be possible to shift the sign bit into a value position and
then isolate it with an AND; the results simply aren't
predictable. This is one of the reasons why scarred veterans
(where do you think the scars came from?) recommend using
unsigned types for bit-walloping.

--
Er*********@sun.com

Apr 14 '06 #5

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

Similar topics

2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
8
by: Rudolf | last post by:
How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.