"Dan." wrote:
hi, i'm sorry to bother you; but i was wondering what the function of the OR
operator was in terms of numbers:
num0 = num1 or num2
Think of it in terms of the actual bits used by
the computer. Any bit position that has a 1 in
either input number will have a 1 in the output
number:
num1 1011 (11) 0000 (0)
num2 OR 0110 (6) OR 0110 (6)
---- ----
num0 1111 (15) 0110 (6)
AND works the opposite; any bit position that
doesn't have both 1's will output 0:
1011 (11) 1111 (15)
AND 0000 (0) AND 0010 (2)
---- ----
0000 (0) 0010 (2)
--------------------------------------------
'Bitmasking', as it's called, can be useful if
you have a lot of yes/no type data to store
really compactly.
Let's say we have an 8-bit variable CarData;
each bit can represent some feature that is
or is not found on a particular car:
stock/enhanced CD player------
sport package--------------- |
power steering------------ | |
power door locks-------- | | |
convertible----------- | | | |
turbocharger-------- | | | | |
leather seats----- | | | | | |
ABS-------------+ | | | | | | |
0 0 0 0 0 0 0 0
Let's say we have a car with a stock CD player
(its CarData bit 1 is 0), and we want to indicate
that it now has the enhanced player (bit 1 = 1).
BUT, we don't want any other car info to change!
if CarData and 1 = 0 then 'test the value of bit 1
CarData = CarData or 1 'CarData bit 1 is now 1
end if
This is how you set a single bit to zero without
changing any other bits - AND it with a zero bit:
10000101 (133)
AND 11111110 (254)
--------
10000100 (132)
if CarData and 1 = 1 then
CarData= CarData and 254 'only the value of bit 1 changes
end if
Clear as mud? ;-)