449,385 Members | 1,803 Online
Need help? Post your question and get tips & solutions from a community of 449,385 IT Pros & Developers. It's quick & easy.

# BITWISE SHIFT question

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.