This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gsubr.c: C << operator question.


>    Gord> (x << 8) >> 8 == x with the top 8 bits cleared

Only true if x is unsigned.

> Ok, so wouldn't it be faster to use (x & 0xff)?

Er, you mean x & 0xffffff?  (Assuming sizeof(x)==4 and x unsigned.)

But the real reason why (x << 8) >> 8 is useful is that it
performs sign extension.  If you have a 24-bit 2's complement
integer in x, and x is a 32-bit *signed* integer variable,
then x << 8 gives you the same 24-bit value, but now in the
high-order 24 bits, and (x<<8)>>8 is the same as the
original value, but where the high-order 8 bits match
the sign bit of the original 24-bit value.

	--Per Bothner
Cygnus Solutions     bothner@cygnus.com     http://www.cygnus.com/~bothner