While I'm almost certain these already exist on the forums., I can't actually find it as part of the Code Snippet/Samples forum.
It of course could be that I'm useless with search engines but it's weird to me that there isn't a code snipper with such.
This provides Bit (or Binary) Flagging Functions.
I need to test on Mobile, as I have a feeling that ARM uses Big Endian (reversed behaviour)... which is only specifically important if you're using these to comparison values (as you'll get the wrong numbers).
Still if you using it just for setting and getting individual bits; it will work just fine as then it doesn't matter about the order.
Function GetBit( Value As Integer, Bit As Integer )
Local Out As Integer
Out = 0x01 && ( Value >> Bit )
EndFunction Out
Function GetByte( Value As Integer, Byte As Integer )
Local Out As Integer
Out = 0xFF && ( Value >> ( Byte * 8 ) )
EndFunction Out
Function GetShort( Value As Integer, Short As Integer )
Local Out As Integer
Out = 0xFFFF && ( Value >> ( Short * 16 ) )
EndFunction Out
Function SetBit( Value As Integer, Bit As Integer )
Local Out As Integer
Out = ( 0x01 << Bit ) || Value
EndFunction Out
Function SetByte( Value As Integer, In As Integer, Byte As Integer )
Local Out As Integer
Out = ( In << ( Byte * 8 ) ) || Value
EndFunction Out
Function SetShort( Value As Integer, In As Integer, Short As Integer )
Local Out As Integer
Out = ( In << ( Short * 16 ) ) || Value
EndFunction Out
Function FlipBit( Value As Integer, Bit As Integer )
Local Out As Integer
Out = ( 0x01 << Bit ) ~~ Value
EndFunction Out
Function UnsetBit( Value As Integer, Bit As Integer )
Local Out As Integer
Out = ( ! ( 0x01 << Bit ) ) && Value
EndFunction Out
Function CreateIntegerfromBytes( Byte0 As Integer, Byte1 As Integer, Byte2 As Integer, Byte3 As Integer )
Local Out As Integer
Out = SetByte( Out, Byte0, 0 )
Out = SetByte( Out, Byte1, 1 )
Out = SetByte( Out, Byte2, 2 )
Out = SetByte( Out, Byte3, 3 )
EndFunction Out
Function CreateIntegerFromShorts( Short0 As Integer, Short1 As Integer )
Local Out As Integer
Out = SetShort( Out, Short0, 0 )
Out = SetShort( Out, Short1, 1 )
EndFunction
Function Absolute( Value As Integer )
Local Out As Integer
If Value < 0 Then Out = !Value Else Out = Value
EndFunction Out
Function Binary( Value As Integer, Length As Integer )
Local Out As String = ""
// Ensure values are in-range
If Length < -32 Then Length = -32
If Length > 32 Then Length = 32
// Check it isn't 0
If Length <> 0
If Length > 0
// Least Significant Bit
For Bit = 0 To Length - 1
Out = Out + Str( GetBit( Value, Bit ) )
Next
Else
// Most Significant Bit
For Bit = Absolute(Length) - 1 To 0 Step -1
Out = Out + Str( GetBit( Value, Bit ) )
Next
EndIf
EndIf
EndFunction Out
Function SetBitRange( Value As Integer, In As Integer, Bit As Integer, Depth As Integer )
Local Out As Integer
Local Offset As Integer : Offset = ( 2 ^ Depth ) - 1
Local Mask As Integer : Mask = !( ( 2 ^ 32 ) ) ~~ ( Offset << Bit )
Out = ( In << Bit ) || Value
EndFunction Out
Function GetBitRange( Value As Integer, Bit As Integer, Depth As Integer )
Local Out As Integer
Local Offset As Integer : Offset = ( 2 ^ Depth ) - 1
Local Mask As Integer : Mask = !( ( 2 ^ 32 ) - 1 ) ~~ ( Offset << Bit )
Out = ( Value && Mask ) >> Bit
EndFunction Out
[edit]
Update • Changed how the Bits work ( 0 - 31 instead of 1 - 32 ) and added 4 new functions Set/GetByte (4x8bit) and Set/GetShort (2x16bit)
These should help when reading files that have 8bit and 16bit values
Update 2 • Added CreateIntegerFromBytes( ) and CreateIntegerFromShorts( )
Update 3 • Added Set/GetBitRange( ) for Custom Packing Data