Some Bus and Video tweaks

This commit is contained in:
Jeff Parsons 2015-01-27 15:46:22 -08:00 committed by jeffpar
commit 2637a4ca85
9 changed files with 290 additions and 168 deletions

View file

@ -28,7 +28,7 @@ result is always extended into the entire 52 "significand" bits of the underlyin
impossible to simply "mask away" those additional sign bits, thanks to the fundamental restriction of JavaScript bitwise
operators: they operate *only* on the low 32 bits.
The easiest way to remove the high-order sign bits from a negative 32-bit value is to add 0x100000000:
The easiest way to remove the high-order sign bits from a negative 32-bit value is to add the 33-bit value 0x100000000:
> n = (n < 0? n + 0x100000000 : n)
2147483648
@ -36,15 +36,26 @@ The easiest way to remove the high-order sign bits from a negative 32-bit value
'80000000'
This works because JavaScript is perfectly capable of representing 0x80000000, or any other 32-bit value, as a positive
number. But be careful, because as soon as you perform *any* bitwise operation on a value with bit 31 set, even
an operation as innocuous-looking as:
number, albeit in floating point. And be careful, because as soon as you perform *any* bitwise operation on a value
with bit 31 set, even an operation as innocuous-looking as:
> n |= 0
-2147483648
> n.toString(16)
'-80000000'
Viola: instant negative number! To continue the fun, set bit 0 of 0x80000000, which should give you 0x80000001:
Viola: instant negative number!
This might tempt you to think that the right way to write negative 32-bit constants in hex is to simply precede
them with a minus sign. But that would be wrong. For example, if you wrote the constant 0x80000080 as "-0x80000080",
JavaScript would treat that as negation of 2147483776, resulting in a value whose low 32 bits are 0x7FFFFF80, not
0x80000080.
The safest way to write a 32-bit constant like 0x80000080 is "0x80000080|0", which will produce -2147483520. If you
write all your negative 32-bit constants that way, then you won't have to resort to 33-bit addition and potential
floating point operations.
To continue the fun, try setting bit 0 of 0x80000000, which should give you 0x80000001:
> n |= 1
-2147483647
@ -54,7 +65,8 @@ Viola: instant negative number! To continue the fun, set bit 0 of 0x80000000, w
WTF? Have all the low 32 bits flipped instead?
Actually, no, this time, I'm pulling your leg. The low 32 bits of the internal value are exactly what you would
expect: 0x80000001 (the internal representation is more like 0xFFFFF80000001). But as the [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)
expect: 0x80000001 (the internal representation is more like 0xFFFFF80000001). But as the
[MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)
explain, for a negative number, toString() returns the positive representation of the number, preceded by a - sign,
*not* the "two's complement" of the number.