'{$STAMP BS2} '{$PBASIC 2.5} ' 2008 mlab.uiah.fi/paja ' Understanding bits 'A bit is a binary digit, taking a value of either 0 or 1. 'FOR example, the number 10010111 is 8 bits long. 'It is a basic unit of information storage and communication in digital. ' ---[ Variables ]--------- bBinary VAR BYTE wBinary VAR WORD ' multiple value cMulti CON 100 ' ---[ Initialization ]---- DEBUG CLS, "start", CR ' ---[ Main Code ]--------- xMain: DEBUG "Decimal: ", DEC 4, " = Binary: ", BIN 4, CR, CR 'Understanding BIN and BIN(n) bBinary = %00001111 DEBUG "Difference btw BIN, BIN5 and BIN8", CR DEBUG "bBinary = %00001111", CR DEBUG "BIN bBinary = ", BIN bBinary, CR DEBUG "BIN5 bBinary = ", BIN5 bBinary, CR DEBUG "BIN8 bBinary = ", BIN8 bBinary, CR, CR DEBUG "Accessing a specific bit in a variable", CR bBinary = %0001001 'BIT8 -> BIT0 DEBUG BIN8 bBinary, CR DEBUG "BIT1: ", BIN bBinary.BIT0, CR DEBUG "BIT8: ", BIN bBinary.BIT7, CR DEBUG "Let's change third bit to 0 in the value!", CR bBinary.BIT3 = 0 DEBUG BIN8 bBinary, CR, CR 'Bit shift (Shift left) bBinary = %11110111 DEBUG "Shift left", CR DEBUG "bBinary = ", BIN8 bBinary, CR DEBUG "bBinary << 1 = ", BIN8 bBinary << 1, CR, CR ' Shift value left idx places 'Bit shift (Shift right) bBinary = %11110111 DEBUG "Shift right", CR DEBUG "bBinary = ", BIN8 bBinary, CR DEBUG "bBinary >> 1 = ", BIN8 bBinary >> 1, " (Look at 8th bit!)", CR, CR ' Shift value left idx places 'Application of the bit shift DEBUG "Shifting right by a bit means dividing with 2", CR bBinary = 20 DEBUG DEC bBinary, " -> " bBinary = bBinary >> 1 DEBUG DEC bBinary, CR, CR 'Application of the bit shift 2 DEBUG "Last bit affects the positiveness or negativeness of a quantity", CR DEBUG "BIN: ", BIN16 %1000000000000001, TAB, "SDEC: ", SDEC %1000000000000001, CR DEBUG "BIN: ", BIN16 %0000000000000001, TAB, "SDEC: ", SDEC %0000000000000001, CR, CR 'Reverse DEBUG "Inverse", CR DEBUG BIN4 %1011, " -> ", BIN4 ~%1011, CR END