AND

Top  Previous  Next

Action

This logical operator returns the AND of two numeric variables.

 

 

Syntax

target = source1  AND source2

 

 

Remarks

The AND operator works on two bits. It returns a '1' if both inputs are '1'.

 

A

B

R

0

0

0

0

1

0

1

0

0

1

1

1

 

The truth table above shows all possible values. A and B represent the 2 inputs. R is the Return or output value. As you can see, you will only get a '1' when both inputs are '1'

It is like having 2 switches in series. You have to switch them both on in order to have a closed circuit.

 

While you can use AND on bits, you can also perform the same operation on bytes, integers, etc. In such a case, all bits of the variables will be AND-ed.

Example :

Dim A as Byte, B as Byte, R as byte

A=&B1100_0001

B=&B1001_0000

 

R=A AND B

 

R=&B1000_0000

 

As you can see, only bit 7 of both variables is '1'. So in the result, only bit 7 is set. This makes the AND operation perfect for isolating or clearing bits. If you want a value to be in a range of say 0-7 you can set the value to 7 : result= var AND &B111

 

 

 

See also

OR , XOR, NOT

 

 

Example

'--------------------------------------------------------------------------------
'name                     : boolean.bas
'copyright                : (c) 1995-2021, MCS Electronics
'purpose                  : demo: AND, OR, XOR, NOT, BIT, SET, RESET and MOD
'suited for demo          : yes
'commercial add on needed : no
'use in simulator         : possible
'--------------------------------------------------------------------------------
'This very same program example can be used in the Help-files for
'      AND, OR, XOR, NOT, BIT, SET, RESET and MOD
 
 
$baud = 19200
$crystal = 8000000
$regfile = "m88def.dat"
 
$hwstack = 40
$swstack = 20
$framesize = 20
 
Dim A As Byte , B1 As Byte , C As Byte
Dim Aa As Bit , I As Integer
 
A = 5 : B1 = 3                                             ' assign values
C = A And B1                                               ' and a with b
Print "A And B1 = " ; C                                     ' print it: result = 1
 
 
C = A Or B1
Print "A Or B1 = " ; C                                     ' print it: result = 7
 
C = A Xor B1
Print "A Xor B1 = " ; C                                     ' print it: result = 6
 
A = 1
C = Not A
Print "c = Not A " ; C                                     ' print it: result = 254
C = C Mod 10
Print "C Mod 10 = " ; C                                     ' print it: result = 4
 
 
If Portb.1 = 1 Then                                         'test a bit from a PORT (which is not the same as testing the input state)
Print "Bit set"
Else
Print "Bit not set"
End If                                                     'result = Bit not set
 
Config Pinb.0 = Input : Portb.0 = 1                         'configure as input pin
Do
Loop Until Pinb.0 = 0                                       ' repeat this loop until the logic level becomes 0
 
Aa = 1                                                     'use this or ..
Set Aa                                                     'use the set statement
If Aa = 1 Then
Print "Bit set (aa=1)"
Else
Print "Bit not set(aa=0)"
End If                                                     'result = Bit set (aa=1)
 
Aa = 0                                                     'now try 0
Reset Aa                                                   'or use reset
If Aa = 1 Then
Print "Bit set (aa=1)"
Else
Print "Bit not set(aa=0)"
End If                                                     'result = Bit not set(aa=0)
 
C = 8                                                       'assign variable to &B0000_1000
Set C                                                       'use the set statement without specifying the bit
Print C                                                     'print it: result = 9 ; bit0 has been set
 
B1 = 255                                                   'assign variable
Reset B1.0                                                 'reset bit 0 of a byte variable
Print B1                                                   'print it: result = 254 = &B11111110
 
B1 = 8                                                     'assign variable to &B00001000
Set B1.7                                                   'set it
Print B1                                                   'print it: result = 9 = &B00001001
End