View Single Post
  #4 (permalink)  
Old May 19th, 2005, 04:14 PM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts

I am terribly sorry if it looks like fault-finding because actually it is not. I am just trying to make the best possible use of the book. So, several notes.

Page 42. Signed right shift
var iNew = iOld >> 5; //equal to binary 10 with is decimal 2
misprint in comment: “with is decimal” instead of “which is decimal”, but may be it’s only in my copy.

Page 42. Unsigned right shift
Is seems to me that the whole following paragraph has some misconception

quote:For numbers that are negative, however, something quite different happens. You see, the unsigned right
shift operator fills all empty bits with the value contained in the 32nd bit. For positive numbers, this bit
is 0; so the empty bits are filled with zero. For negative numbers, however, this bit is 1, meaning that all
empty bits are filled with 1. Because the result of unsigned right shift is an unsigned 32-bit number, you
end up with a very large number. For example, if you shift –64 to the right by five bits, you end up with
2147483616. How does this happen?
As far as I understand unsigned right shift operator (as opposed to signed right shift) fills empty bits with zeros. In other words it doesn’t fill them at all.
And at any rate if you shift -64 to the right by five bits with unsigned right shift operator, you end up with 134217726, not 2147483616.

And further

Page 43
quote:First, look at the true 32-bit representation of –64. To do so, you need to create an unsigned version of the
number, which can be attained by using unsigned right shift with a bit count of 0:
var iUnsigned64 = 64 >>> 0;
Then, to get the actual bit representation, use the toString() method of the Number type with a
radix of 2:
I think a minus was omitted in expression “var iUnsigned64 = 64 >>> 0;”. It should be “var iUnsigned64 = -64 >>> 0;”.
We are talking about “the true 32-bit representation of –64.”, not 64.
Otherwise following “alert(iUnsigned64.toString(2));” will yields 1000000, not 11111111111111111111111111000000.

Finally (concerning my bad English I am quite uncertain) in my opinion in phrase “This yields a value of 11111111111111111111111111000000, which is the two’s complement representation of –64 for a signed integer, …” it would be better to say “which is two’s complement representation of 64”, because logically the binary number mentioned above is a two’s complement representation of 64, not -64. But once again may be I am wrong.

Best regards

Reply With Quote