Prev | Current Page 145 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

This is because your values occupy the last 8 bits of each
integer, so when they are assigned to your new variable, they are simply transferred to the last 8 bits of
the new integer. What you need to do instead is to shift the bits, so instead of assigning to the last 8 bits,
they are assigned to the next free group of 8 bits.
The answer is to use the bitwise shift operators, which allow you to left shift ( < < ) or right shift ( > > ) bits
by a specified number of places. Therefore, to shift the group of bits four places to the left, you could
write:
myContainer = myBits < < 4;
Part I: The Core Language
62
This is the equivalent of:
00000000 00000000 00000000 01010000 < < 4
=
00000000 00000000 0000 0101 0000 0000
As you can see, when shifting your bits left, zeros were added to the right of your value to keep the
number of bits to 32. Had there been 1 ??™ s occupying the last 4 bits on the left, they would have been lost
in the shift. Shifting your bits left alters your value by making it larger.


Pages:
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157