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

# small bit shifting q

 P: n/a hiyer, say you've got a 32 bit int - is there a nifty way to shift that whole value right by 4 bits and at the same time collect the 4 bits that are being shifted off the edge to the right into another variable? i'm hoping for a way to do it in one foul swoop ... ? probably not, but just wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4; thanks, ben. Nov 14 '05 #1
4 Replies

 P: n/a On Thu, 04 Mar 2004 23:10:58 +0000, ben wrote: hiyer,say you've got a 32 bit int - is there a nifty way to shift that wholevalue right by 4 bits and at the same time collect the 4 bits that arebeing shifted off the edge to the right into another variable? i'mhoping for a way to do it in one foul swoop ... ? probably not, butjust wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4; I'd have to agree with "probably not". You could do it in one statement by employing severe operator abuse (of the comma operator), but it wouldn't gain you anything. In fact, I'll make a bold prediction: if anyone /does/ come up with a way to do it more efficiently, even then the total CPU time that would be saved by the "improved" version would be dwarfed by the amount of brain-time spent in pondering the question ;-) -leor thanks, ben. Leor Zolman BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix C++ users: Download BD Software's free STL Error Message Decryptor at www.bdsoft.com/tools/stlfilt.html Nov 14 '05 #2

 P: n/a In article , Leor Zolman wrote: On Thu, 04 Mar 2004 23:10:58 +0000, ben wrote:hiyer,say you've got a 32 bit int - is there a nifty way to shift that wholevalue right by 4 bits and at the same time collect the 4 bits that arebeing shifted off the edge to the right into another variable? i'mhoping for a way to do it in one foul swoop ... ? probably not, butjust wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4; I'd have to agree with "probably not". You could do it in one statement by employing severe operator abuse (of the comma operator), but it wouldn't gain you anything. In fact, I'll make a bold prediction: if anyone /does/ come up with a way to do it more efficiently, even then the total CPU time that would be saved by the "improved" version would be dwarfed by the amount of brain-time spent in pondering the question ;-) ok fair enough :) it just seemed slightly logical that there might be a way to do that in one go. not to worry - thanks for confirming. ben. Nov 14 '05 #3

 P: n/a ben wrote: hiyer, say you've got a 32 bit int - is there a nifty way to shift that whole value right by 4 bits and at the same time collect the 4 bits that are being shifted off the edge to the right into another variable? i'm hoping for a way to do it in one foul swoop ... ? probably not, but just wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4; thanks, ben. This is a common technique in assembly languages, to rotate a bit from one number to another for multiprecision numbers. I haven't seen any assembly languages that offer this capability. Most will shift a bit into carry and shift in a bit from carry. I've had to repeat this process in a loop. But there are better methods. -- Thomas Matthews C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html Other sites: http://www.josuttis.com -- C++ STL Library book Nov 14 '05 #4

 P: n/a "Thomas Matthews" wrote in message news:0D******************@newssvr31.news.prodigy.c om... ben wrote: hiyer, say you've got a 32 bit int - is there a nifty way to shift that whole value right by 4 bits and at the same time collect the 4 bits that are being shifted off the edge to the right into another variable? i'm hoping for a way to do it in one foul swoop ... ? probably not, but just wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4; thanks, ben. This is a common technique in assembly languages, to rotate a bit from one number to another for multiprecision numbers. I haven't seen any assembly languages that offer this capability. Most will shift a bit into carry and shift in a bit from carry. I've had to repeat this process in a loop. But there are better methods. The 32-bit IBM Mainframe (S/390) has a double-register shift that is used for rotating the bits within a 32-bit register. The shifted-out bits go into an adjacent register, instead of the bit-bucket. The next instruction is a bitwise OR that copies the shifted-out bits back into the source register at the other end. Rotating a 32-bit operand requires 2 adjacent registers (even/odd numbered) and 3 instructions (including one instruction to clear the register receiving the shifted-out bits). The 64-bit IBM Mainframe (z/Architecture) added a rotate instruction for both 32-bit and 64-bit operands, so the double register shifting technique is no longer needed (but it still works for 32-bit values). One instruction to rotate the bits, and the rotated value can land in a different target register without altering the source register. Having said that, I cannot think of a C idiom that would represent succinctly such an operation of rotating bits (or extracting bits and shifting the other bits) for a 32-bit quantity. The best I can think of is the OP's example. -- ---------------------------- Jeffrey D. Smith Farsight Systems Corporation 24 BURLINGTON DRIVE LONGMONT, CO 80501-6906 http://www.farsight-systems.com z/Debug debugs your Systems/C programs running on IBM z/OS! Are ISV upgrade fees too high? Check our custom product development! Nov 14 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.