Roger,
Thanks for your input. I really appreciate the time you took to write this response. I have done a lot of bit masking. I have only used the logical operators(&, |, ^,...) for masking. I can see in special cases a shift operator maybe more efficient.
I was intrigued by your small integer arithmetic. I do not see how this would be faster. It seems to me, the trips to the CPU needed to pack and unpack, would balance out against arithmetic operations. I have no expertise at low level programming so any input on your part would be appreciated.
Again Thanks
-----Original Message-----
From: Roger Glover [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 05, 2002 3:34 PM
To: JDJList
Subject: [jdjlist] Re: Shift Operator: What is it good for?
There is no doubt that in C-based languages the shift operators, like "bitwise
logic" operators, fall deeply into that twilight zone of program "bit
twiddling". Remember that the first "application" written in the C language
was the Unix kernel, so the developers of the language were deeply attached to
such things. However, the shift operators have found *some* usage outside of
low level systems programming.
David's mentioned of bit masking operations, which is, of course the original
motivating reason for creating the operators in the first place. In most
application programming, especially in Java, there tends to be very little need
for this. However, there are some special purpose usages that are fairly
interesting to application programmers.
For example, there is also a little known usage for compressed small integer
arithmetic. It turns out that you can fit 5 8-bit numbers into a 64-bit long
(the other space is used for sign correction and overflow control), and do add
and subtract arithmetic on them. On 64-bit architectures, you then get 5
arithemetic operations for the price of one. In this very esoteric usage, the
shift operations are only needed for packing and unpacking the 5 8-bit numbers.
However, probably the most common application programming usage of shift
operators in C-based languages (outside C++) is for special case multiplication
and division. Left shift is a fast way to multiply an integer by a power of 2,
while right shift is an equally fast way to divide an integer by a power of 2.
As for C++, far and away the most common usage of the shift operators is for,
believe it or not, I/O. :-P :-P :-P :-P :-P :-P :-P :-P :-P :-P :-P :-P :-P
Left and right shift also have many, many other fascinating uses in the field
of cryptography, but very few of us have opportunity to work on such apps.
-- Roger Glover
--- David Rosenstrauch <[EMAIL PROTECTED]> wrote:
> At 02:19 PM 9/5/2002 -0400, you wrote:
>
> >During the discussion about 'Getting digits from an integer' there was a
> >reference to the binary shift operator >>. In my 15 years of programming
> >I have never once used the shift operator. I am sure it must be used for
> >something, but I can not think of anything useful. Anybody out there
> >every use it? If so, what for?
> >
> >Thanks
> >Knud Hansen
>
>
> I think it's most commonly used in applying bit-masks (e.g., when you want
> to strip out the "red" value out of RGB color integer values).
>
>
> DR
=====
-- Roger Glover
__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm