"Thomas Matthews" <Th****************************@sbcglobal.net> 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!