Mail Archives: djgpp/1998/06/29/14:47:54
>> In almost any language, this is correct. It's faster to shift bits along
a
>> byte than it is to multply, carry, check for overflows etc. It's much
faster
>> to divide by using >> or SHR.
>
>Well you mean for most cpus not languages. In a AMD 686 a multiply is
almost
>as fast a shift. (well except it doesn't pair)
"Almost" is a long time when you're talking in clock cycles :^)
>> I have noticed, though, that the PC only has one type of shifting
command.
>> On the Motorola, there's ASL, ASR (arethmetic), LSL and LSR (logical),
along
>> with the rolling commands ROL and ROR and all the shifting commands that
>> change different bits depending on the values entered into them. Are
there
>> any of these on the PC?
>
>You can shift (on a 286) a byte or word or dword (386) any value, not just
one.
>On a 8086 you must put the shift count in cl.... read a doc on 8086 for
more.
>
>> And while we're on the subject, I am assuming that << and >> are
arethmetic
>> shifting operands. Is there a similar command to do logical shifting in
>> C/C++?
>
>In C you use << SHL and >> SHR
You don't seem to understand what I mean. There is a fundamental difference
between arethmetic and logical shifting. Arethmetic shifting shifts the data
(bytes, words or longs) but keeps the sign bit intact. Logical shifting
treats the sign bit as any other bit in the byte/word/long.
So for instance, aretmetically shifting left the value -2 would give the
result -4; logically shifting left -2 would give 4.
Rolling takes any bits shifted off one end of the byte/word/long and sticks
them on the other end.
So rolling a byte right (logically) with the value of 1 would give the
answer of 65664.
The shift commands which change different bits that I was referring to are
commands like "shift with carry" and "shift with overflow."
AFAIK, you can only do arethmetic shifting in C, like I said.
James Arthur
jaa AT arfa DOT clara DOT net
- Raw text -