Code: Select all
EnableExplicit
;ABOUT BINARY, BITS AND BYTES
;Computers work with binary numbers.
;Binary numbers are build up with Bits.
;A singular Bit can either have the value of one or zero.
;The computer understands this as current and no current.
;A Byte consists of 8 Bits or 2 Nibbles (4 Bits) and can represent 256 numbers.
;(4 * 4) * (4 * 4) = 256
;The binary representation of an Byte could look like this (8 Bits or 2 Nibbles):
;0110 1100
;The smalles binary number will look like this:
;0000 0000
;And the biggest like this:
;1111 1111
;128 would look like this:
;1000 0000
Debug "#Example 1 - The Bits of 128:"
Debug "10000000 = " + Str(%10000000)
Debug ""
;And 1 would be:
;0000 0001
Debug "#Example 2 - The Bits of 1:"
Debug "00000001 = " + Str(%00000001)
Debug ""
;The decimal value of every Bit in a Byte:
;128 64 32 16 8 4 2 1
;So what is the value of 0101 1010?
; 0 1 0 1 1 0 1 0
;(0 * 128 = 0) + (1 * 64 = 64) + (0 * 32 = 0) + (1 * 16 = 16) + (1 * 8 = 8) + (0 * 4 = 0) + (1 * 2 = 2) + (0 * 1 = 0) = 90
Debug "#Example 3 - The value of a Byte:"
Debug RSet(Bin(90,#PB_Ascii),8,"0") + " = 90"
Debug ""
;Code Example:
Debug "#Example 4 - Code example:"
Procedure.i ByteValue(Binary.s)
Protected index.i
Protected count.i
Protected value.i
Binary = ReverseString(Binary)
count = 1
For index = 1 To 8
value + Val(Mid(Binary,index,1)) * count
count + (1 + index) % 1 + count;<- calculate 2, 4, 8, 16, 32, 64, 128
Next
ProcedureReturn value
EndProcedure
Debug "11001101 = " + Str(ByteValue("11001101"))
Debug "11001101 = " + Str(%11001101)
Debug "11111101 = " + Str(ByteValue("11111101"))
Debug "11111101 = " + Str(%11111101)
Debug ""
;Conversion table for a Nibble (Binary, Decimal, Hexadecimal):
;--------------------------
;| BIN: | DEC: | HEX: |
;--------------------------
;| 0000 | 0 | 0 |
;| 0001 | 1 | 1 |
;| 0010 | 2 | 2 |
;| 0011 | 3 | 3 |
;| 0100 | 4 | 4 |
;| 0101 | 5 | 5 |
;| 0110 | 6 | 6 |
;| 0111 | 7 | 7 |
;| 1000 | 8 | 8 |
;| 1001 | 9 | 9 |
;| 1010 | 10 | A |
;| 1011 | 11 | B |
;| 1100 | 12 | C |
;| 1101 | 13 | D |
;| 1110 | 14 | E |
;| 1111 | 15 | F |
;--------------------------
;The decimal value for every Hexadecimal in a Word (2 Bytes):
;4096 256 16 1
;So what is the value of 029A?
; 0 2 9 A
;(0 * 4096 = 0) + (2 * 256 = 512) + (9 * 16 = 144) + (10 * 1 = 10) = 666
;Ways for a programmer to interact with Bits:
;For example Bit shifting moves the Bits to the left or right (Bit shifting operators << and >>):
Debug "#Example 5 - Bit shifting:"
Debug "00010000 << 1 = " + ReplaceString(RSet(Bin(%00010000 << 1,#PB_Ascii),8,"0"),"10","1<-0") + " SHIFT LEFT"
Debug "00010000 >> 1 = " + ReplaceString(RSet(Bin(%00010000 >> 1,#PB_Ascii),8,"0"),"01","0->1") + " SHIFT RIGHT"
Debug ""
;What is it good for?
;It can be used to determine the state of a single Bit or for very fast multiplications and divisions.
Debug "#Example 6 - Fast multiplication and division:"
Debug "1 << 1 = " + Str(1 << 1)
Debug "1 << 2 = " + Str(1 << 2)
Debug "2 >> 1 = " + Str(2 >> 1)
Debug "4 >> 1 = " + Str(4 >> 1)
Debug ""
;More operators from the manual:
;Bitwise OR (|):
;The result of this operator will be the value of the expression on the LHS or'ed with the value of the expression on the RHS, bit for bit.
;The value of each bit is set according to the table below. Additionally,
;if the result of the operator is not used and there is a variable on the LHS,
;then the result will be stored directly in that variable. This operator cannot be used with strings.
; LHS | RHS | Result
; ------------------
; 0 | 0 | 0
; 0 | 1 | 1
; 1 | 0 | 1
; 1 | 1 | 1
; Bitwise AND (&):
;The result of this operator will be the value of the expression on the LHS anded with the value of the expression on the RHS, bit for bit.
;The value of each bit is set according to the table below. Additionally, if the result of the operator is not used and there is a variable on the LHS,
;then the result will be stored directly in that variable. This operator cannot be used with strings.
; LHS | RHS | Result
; ------------------
; 0 | 0 | 0
; 0 | 1 | 0
; 1 | 0 | 0
; 1 | 1 | 1
; Bitwise XOR (!):
;The result of this operator will be the value of the expression on the LHS xor'ed with the value of the expression on the RHS, bit for bit.
;The value of each bit is set according to the table below. Additionally, if the result of the operator is not used and there is a variable on the LHS,
;then the result will be stored directly in that variable. This operator cannot be used with strings.
; LHS | RHS | Result
; ------------------
; 0 | 0 | 0
; 0 | 1 | 1
; 1 | 0 | 1
; 1 | 1 | 0
; Bitwise NOT (~):
;The result of this operator will be the not'ed value of the expression on the RHS, bit for bit.
;The value of each bit is set according to the table below. This operator cannot be used with strings.
; RHS | Result
; ----------
; 0 | 1
; 1 | 0
;Useful macros for Bits:
Macro BitGet(_var_,_bit_)
((_var_) >> (_bit_)) & 1
EndMacro
Macro BitSet(_var_,_bit_)
_var_ | (1 << (_bit_))
EndMacro
Macro BitReset(_var_,_bit_)
_var_ & ~ (1 << (_bit_))
EndMacro
Procedure.i BitMacros()
Protected int.i
int = %100010110000111100001101
Debug "BitGet(" + Bin(int) + ",X)"
Debug "Bit POS 0 = " + Str(BitGet(int,0)) + " 10001011000011110000110(1)"
Debug "Bit POS 19 = " + Str(BitGet(int,19)) + " 1000(1)0110000111100001101"
Debug "Bit POS 22 = " + Str(BitGet(int,22)) + " 1(0)0010110000111100001101"
Debug "BitSet(" + Bin(int) + ",X)"
BitSet(int,22)
Debug "Bit SET 22 = " + Str(BitGet(int,22))
Debug "BitReset(" + Bin(int) + ",X)"
BitReset(int,22)
Debug "Bit RESET 22 = " + Str(BitGet(int,22))
ProcedureReturn #Null
EndProcedure
Debug "#Example 6 - Bit Macros:"
BitMacros()
Debug ""