473,383 Members | 1,984 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,383 software developers and data experts.

BITWISE SHIFT question

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?

Apr 13 '06 #1
13 2275
On Wed, 12 Apr 2006 22:52:04 -0700, HARDCORECODER wrote:
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.


Apr 13 '06 #2

noone wrote:
On Wed, 12 Apr 2006 22:52:04 -0700, HARDCORECODER wrote:
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!


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.

Apr 13 '06 #3
HARDCORECODER wrote:
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

Apr 13 '06 #4
Maxim Yegorushkin wrote:
[...]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #5
Mi*************@tomtom.com 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"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #6
Victor Bazarov wrote:
Mi*************@tomtom.com 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_?

Apr 13 '06 #7
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?

Apr 13 '06 #8
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?

Apr 13 '06 #9
Markus Schoder wrote:
Victor Bazarov wrote:
Mi*************@tomtom.com 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?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #10
HARDCORECODER wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #11
Victor Bazarov wrote:
Markus Schoder wrote:
Victor Bazarov wrote:
Mi*************@tomtom.com 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.

Apr 13 '06 #12
On Thu, 13 Apr 2006 14:04:13 -0400, Victor Bazarov wrote:
OK. How do you define how much to shift?
I think he is right what do you think?


Who is "he"?


noone of consequence...
Apr 14 '06 #13
On Thu, 13 Apr 2006 09:43:58 -0400, Victor Bazarov wrote:
Mi*************@tomtom.com 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"?


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!


Apr 14 '06 #14

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...
34
by: Christopher Benson-Manica | last post by:
I'm trying to compute the absolute value of an integer using only bitwise operators... int x; sscanf( "%d", &x ); printf( "%d\n", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) ); That works, but it...
3
by: sandy_pt_in | last post by:
Hi C guru's can you please tell me how to do multiplication of 2 numbers using bitwize operators?? expecting reply.
13
by: Manish_Ganvir | last post by:
Please do not use pointer arithmetic or for loops Solution
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...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.