small bit shifting q

B

ben

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.
 
L

Leor Zolman

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;

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
(e-mail address removed)
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
 
B

ben

Leor Zolman said:
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.
 
T

Thomas Matthews

ben said:
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
 
X

xarax

Thomas Matthews said:
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!
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top