BITWISE SHIFT question

H

HARDCORECODER

ok here is the question. I want to exract the first 4 bits in a int
so let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter
to the left right? :) and if I'm wrong how can i get an x number of
bits into a variable?
 
N

noone

ok here is the question. I want to exract the first 4 bits in a int so
let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter to
the left right? :) and if I'm wrong how can i get an x number of bits into
a variable?

"first four bits" is ambiguous...and depends upon whether your system is
big or little endian...and as everyone knows, little endian machines suck!

but, to answer your question, shift left increases the value by a power
of two and shift right decreases by a power of two....so, to extract bit
fields out of a number with bits numbers 2^n n=[0..15] use

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

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

to extract bit fields you should shift right then use a bit mask
containing all ones.
 
M

Maxim Yegorushkin

noone said:
"first four bits" is ambiguous...and depends upon whether your system is
big or little endian...and as everyone knows, little endian machines suck!

In C and C++ bits are counted right to left no matter what hardware bit
order is. N's bit thus can be extracted by shifting 1 right N times and
using the value as a bit-mask:

unsigned x;
unsigned n;
unsigned bit_n = 0 != x & (1 << n);

Don't forget that in C and C++ we count from 0, so that the lowest
order bit has index 0.
 
M

Michiel.Salters

HARDCORECODER said:
ok here is the question. I want to exract the first 4 bits in a int
so let say

int b = somenumber;
int result= 0;
result = b << 4;

if I got this right result should contain the 4 bits that were shifter
to the left right? :) and if I'm wrong how can i get an x number of
bits into a variable?

Nope. b << 4 just means b * 16, and b >> 4 means b / 16.

What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

HTH,
Michiel Salters
 
V

Victor Bazarov

Maxim said:
[...]
In C and C++ bits are counted right to left no matter what hardware
bit order is. N's bit thus can be extracted by shifting 1 right N

You mean "by shifting 1 *LEFT* N times", of course...
times and using the value as a bit-mask:

unsigned x;
unsigned n;
unsigned bit_n = 0 != x & (1 << n);

And here you're shifting _left_, BTW.
Don't forget that in C and C++ we count from 0, so that the lowest
order bit has index 0.

V
 
V

Victor Bazarov

[..]
What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

"The first four bits" are the bits numbered 0 through 3, no? Aren't
any "first" items always the ones with lower indices? How can they be
the "highest"?

V
 
M

Markus Schoder

Victor said:
[..]
What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

"The first four bits" are the bits numbered 0 through 3, no? Aren't
any "first" items always the ones with lower indices? How can they be
the "highest"?

If you write done a number which digits do you write down _first_?
 
H

HARDCORECODER

ok I meant the first 4 bits....bit 0,1,2,3. I want to exract these and
place them and place them into another variable.

for example noone wrote:

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

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

to extract bit fields you should shift right then use a bit mask
containing all ones.

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

HARDCORECODER

lets say I have a 32-bit int.

int x =555;

I want to extract the first 4 bits. thats bit 0,1,2,3 and place them
into another variable.

noone wrote this anwser:

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

or

int bits12to15=(x>>12)&0x0f; // bits 12 13 14 15

to extract bit fields you should shift right then use a bit mask
containing all ones.

I think he is right what do you think?
 
V

Victor Bazarov

Markus said:
Victor said:
[..]
What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

"The first four bits" are the bits numbered 0 through 3, no? Aren't
any "first" items always the ones with lower indices? How can they
be the "highest"?

If you write done a number which digits do you write down _first_?

How is that relevant? If I write a number with leading zeros, would you
count those zeros as "first"? Are "00010" and "010" the same or not?
When you put on your pants, do you start with the left led or the right?

V
 
V

Victor Bazarov

HARDCORECODER said:
lets say I have a 32-bit int.

int x =555;

If you meant to give an example of a 32-bit int, I think you missed
a few bits.
I want to extract the first 4 bits. thats bit 0,1,2,3 and place them
into another variable.

Do you want to place them in exact order they are in? Or do you want to
scatter them randomly?
noone wrote this anwser:

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

or

int bits12to15=(x>>12)&0x0f; // bits 12 13 14 15

to extract bit fields you should shift right then use a bit mask
containing all ones.

OK. How do you define how much to shift?
I think he is right what do you think?

Who is "he"?

V
 
M

Markus Schoder

Victor said:
Markus said:
Victor said:
(e-mail address removed) wrote:
[..]
What are "the first four bits" anyway? Those with the lowest value?
Or the highest? And what if there are padding bits?

"The first four bits" are the bits numbered 0 through 3, no? Aren't
any "first" items always the ones with lower indices? How can they
be the "highest"?

If you write done a number which digits do you write down _first_?

How is that relevant? If I write a number with leading zeros, would you
count those zeros as "first"? Are "00010" and "010" the same or not?
When you put on your pants, do you start with the left led or the right?

I was explaining why people might refer to the highest bits as "first"
since at least in Western civilization it is customary to read/write
the highest digits first. What is so difficult to understand about
that?

I fail to see the relevance of your questions though.
 
N

noone

[..]
What are "the first four bits" anyway? Those with the lowest value? Or
the highest? And what if there are padding bits?

"The first four bits" are the bits numbered 0 through 3, no? Aren't any
"first" items always the ones with lower indices? How can they be the
"highest"?

In the embedded world we are frequently concerned with endian issues. On
a big endian machine numbers are stored as god intended, highest order
bits in lowest ordered memory. Then Intel goes and pucks up everything by
making the 80x86 platform little endian so that the least significant bits
come first. On some architectures it is even more archaic: 32 bit
integer representation on a 16 bit micro-controller. Some of them split
the number into 16 bit words and swap the bytes within those words...so,
it's not as cut and dry and I would ask anyone looking for information
about bits to give me the bit numbers on a scale 2^n n=[0..z].

My headache with this issue currently is that some morons designed a
robotics messaging protocol based on a little endian format and we must
design code that works on both big and little endian machines. I cannot
even use bitfields in my structs to pull the data because the protocol
violates network byte ordering of ethernet. I have to code friggin
accessor functions for every stinking field in every stinking type of
message I want to support...talk about archaic!
 

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,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top