Bits of a byte?
BlitzMax Forums/BlitzMax Beginners Area/Bits of a byte?
| ||
How would I get the individual 8 bits from a byte? Like:bit1=howeveryoudoit bit2=howeveryoudoit bit3=howeveryoudoit bit4=howeveryoudoit etc... |
| ||
bit1 = n & %1 bit2 = n & %10 bit3 = n & %100 bit4 = n & %1000 |
| ||
That's a safe way of doing it? No Shr or Shl? |
| ||
true. but there are going to be times when you'll need to use the shift. |
| ||
... Then, what's an all-the-time-good way of retrieving the bits in a byte? |
| ||
bit1 = (n Shr 0) & %1 bit2 = (n Shr 1) & %1 bit3 = (n Shr 2) & %1 bit4 = (n Shr 3) & %1Obviously, the bit indices themselves depend on how you're numbering your bits (from LSB->MSB in this case) and maybe even the endian-ness of the processor architecture. But to all intents and purposes, this should be what you are after. :-) |
| ||
Great! And I could always slip in a ?LittleEndian if needed. :) Thanks! |
| ||
SebHoll's way is correct and usefull if that is what you need. but it is not allways necesary to shift. With the speed of modern processors you probably would not notice any difference with the shift and with out it anyway. I sometimes do something like this:const lft:byte = %100 const cent:byte = %010 const rght:byte = %001 . . . gunsInSHip = lft|rght . . . if (gunsInShip & lft) then shoot left gun if (gunsInShip & rgt) then shoot right gun if (gunsInSHip & cent) then shoot center gun obviously the ship will shoot the left and the right gun. this works with out having to shift any bits. and logically this way is faster in this case. |
| ||
Some Old Timey Pre-School Methods....SuperStrict Local n:Byte, bit7:Byte, bit6:Byte, bit5:Byte, bit4:Byte, bit3:Byte, bit2:Byte, bit1:Byte, bit0:Byte Local bitty:Byte[8] n=195 'Old School Method #1 - High Bit -> Low Bit (Normal Representation) bit7=(n/128) Mod 2 bit6=(n/64) Mod 2 bit5=(n/32) Mod 2 bit4=(n/16) Mod 2 bit3=(n/8) Mod 2 bit2=(n/4) Mod 2 bit1=(n/2) Mod 2 bit0=(n/1) Mod 2 Print "Byte value -> "+n+" = "+bit7+bit6+bit5+bit4+bit3+bit2+bit1+bit0+" bits" Print "~n" 'Old School Method #2 - High Bit -> Low Bit (Normal Representation) (Good for array) Local temp:Byte=n For Local i:Int = 7 To 0 Step -1 If 2^i <= temp Then bitty[i]=1 temp=temp-(2^i) Else bitty[i]=0 End If Next Print "Byte Value -> "+n+" = " For Local i:Int = 7 To 0 Step -1 Print bitty[i] Next Print "bits." :() |
| ||
Here are some usefull functions found right in the code archives. This is from S|F.... Const BIT_0 = 1 Const BIT_1 = 2 Const BIT_2 = 4 Const BIT_3 = 8 Const BIT_4 = 16 Const BIT_5 = 32 Const BIT_6 = 64 Const BIT_7 = 128 Function SetBit(B:Byte Var, Bit:Int) B :| Bit End Function Function ClearBit(B:Byte Var, Bit:Int) B :- Bit End Function Function GetBit:Int(B:Byte, Bit:Int) Return B & Bit > 0 End Function ' - TEST CODE, DELETE THIS. Global Test:Byte = 0 SetBit(Test, BIT_0) SetBit(Test, BIT_2) SetBit(Test, BIT_4) SetBit(Test, BIT_6) ClearBit(Test, BIT_6) Print("BIT_0: " + GetBit(Test, BIT_0) ) Print("BIT_1: " + GetBit(Test, BIT_1)) Print("BIT_2: " + GetBit(Test, BIT_2)) Print("BIT_3: " + GetBit(Test, BIT_3)) Print("BIT_4: " + GetBit(Test, BIT_4) ) Print("BIT_5: " + GetBit(Test, BIT_5)) Print("BIT_6: " + GetBit(Test, BIT_6)) Print("BIT_7: " + GetBit(Test, BIT_7)) :) |