473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2321
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

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

Similar topics

2
3282
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. Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is a sign propagating bit shift. My understanding is that the python >> is equivalent to the javascript >> - but python has no equivalent to >>>. Would a >>> 3 in javascript be...
11
9029
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 anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110) in...
34
16882
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 assumes 32 bit integers. Is there a portable/standard way to do this? Or are ANSI integers always 32 bits? If not, is using sizeof(int) * 8 - 1 as the shift value an acceptable alternative?
3
5812
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
8270
by: Manish_Ganvir | last post by:
Please do not use pointer arithmetic or for loops Solution
8
8419
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
5804
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
5
3316
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 OR) ~x Bitwise negation
5
6819
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. Accordingly, if i need the program to work fine in a little-endian system. I understand that the code needs to be changed. ( I couldn't find any statement in C90 about endianness, hence i'm assuming that c programs are not portable if the endianness...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
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 we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.