| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: the OR operator |
"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? ;-)
---
þ RIMEGate(tm)/RGXPost V1.14 at BBSWORLD * Info{at}bbsworld.com
---
* RIMEGate(tm)V10.2áÿ* RelayNet(tm) NNTP Gateway * MoonDog BBS
* RgateImp.MoonDog.BBS at 6/5/04 2:18:14 PM
* Origin: MoonDog BBS, Brooklyn,NY, 718 692-2498, 1:278/230 (1:278/230)SEEN-BY: 633/267 270 @PATH: 278/230 10/345 106/1 2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.