Liberty BASIC Help Online

Bitwise Operations
What are bitwise operations?
Bitwise operations modify the pattern of bits in an object. Computers use binary numbers. A binary number consists of one or more digits, which represent two different states, such as on/off.
 
An example of a binary number looks like this:
 
10010
 
The first digit on the RIGHT side of the number is in the one's column.  The second digit from the right is in the two's column, then comes the four's column, the eight's column, the sixteen's column and so on.  Each column contains a "bit", or binary digit.
 
16  8  4  2  1 
 
This example binary number, "10010", converted to a decimal number, evaluates like this.  
 
168421
10010
 
16 * 1   = 16
8 * 0   =  0
4 * 0   =  0
2 * 1   =  2
1 * 0   =  0
_________________________________
total    =  18 in decimal numbers
 
It is possible to operate on the bits using BOOLEAN operators.
 
 
AND
The AND operator will set a bit only if both input bits are set.
 
'Bitwise AND
'This operation sets a bit only if
'both inputs have the bit set.
print 7 and 11 ' yields 3
'16 8 4 2 1 '
'-------------- '
' 0 0 1 1 1 ' 7 in binary
' 0 1 0 1 1 ' 11 in binary
'
'       ^ ^ ' These are the place values where both numbers are 1.
' ' Hence, 2 + 1 = 3
 
 
OR
The OR operator will set a bit if either input bit is set.
 
'Bitwise OR
'This operation sets a bit if
'either input has the bit set.
print 7 or 11 ' yields 15
'16 8 4 2 1 '
'-------------- '
' 0 0 1 1 1 ' 7 in binary
' 0 1 0 1 1 ' 11 in binary
'
'   ^ ^ ^ ^ ' These are the place values where either number has a 1.
' ' Hence, 8 + 4 + 2 + 1 = 15
 
 
 
XOR
The Bitwise XOR operation sets a bit only if exactly one of the inputs has the bit set.
 
'Bitwise XOR
'This operation sets a bit only if
'exactly one of the inputs has the bit set.
print 7 xor 11 ' yields 12
'16 8 4 2 1 '
'-------------- '
' 0 0 1 1 1 ' 7 in binary
' 0 1 0 1 1 ' 11 in binary
'
'   ^ ^ ' These are the place values where exactly one of the
' ' numbers has a 1. Hence, 8 + 4 = 12
 
 
See also:  BOOLEAN


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/