Shifting bits in a byte
BlitzMax Forums/BlitzMax Beginners Area/Shifting bits in a byte
| ||
Does anyone know how to shift bits to the left or right in a byte so that they 'wrap' eg: 1111 0000 shifted to the left 4 times would give: 0000 1111 The shl and shr operators 'lose' any bits that fall off the left or right side respectively. |
| ||
Nope. Guess you just have to write a function to do so. |
| ||
Umm .... Local Number=%11110000 '(Is this a binary number, I forget if % is right) Local Number2=Number Local BitPlacesToShift:Int=2 Number:Shr BitPlacesToShift Number2:Shl 8-BitPlacesToShift Number:|Number2 Something like that, the manual way. This is only for a right shifting roll, you'd probably need another version to move left. |
| ||
Thanks AD, I'll have a look at that later. Somewhat better than the awful, awful hack that I came up with: Local b:Byte=2 Local nb:Byte shift=2 For i=0 To 7 If (b & Int(Ceil(2^i))) nb :| Int(Ceil(2^(wrapBit(i,shift)))) Next Print Bin(b) Print Bin(nb) Function wrapBit(bitIndex,bitShift) bitIndex :+ bitShift If bitIndex > 7 bitIndex = (bitIndex-8) Return bitIndex End Function Ahem. |