Skip to main content

AND

Syntax

number AND number

Description

And, at its most primitive level, is a boolean operation, a logic function that takes in two bits and outputs a resulting bit. If given two bits, this function returns true if both bits are true, and false for any other combination. The truth table below demonstrates all combinations of a boolean and operation:

Bit1 Bit2 Result

0 0 0

1 0 0

0 1 0

1 1 1

This holds true for conditional expressions in ARMbasic . When using "And" encased in an If block, While loop, or Do loop, the output will behave quite literally:

IF condition1 AND condition2 THEN expression1

Is translated as:

IF condition1 IS true, AND condition2 IS true, THEN perform expression1

When given two expressions, numbers, or variables that return a number that is more than a single bit, AND is performed "bitwise". A bitwise operation compares each bit of one number, with each bit of another number, performing a logic operation for every bit. The boolean math expression below describes this:

00001111 AND

00011110

-------- equals

00001110

Notice how in the resulting number of the operation, reflects an AND operation performed on each bit of the top operand, with each corresponding bit of the bottom operand. The same logic is also used when working with conditions.

Example

' Using the AND operator on two numeric values

numeric_value1 = 15 '00001111

numeric_value2 =

30 '00011110

'Result = 14 = 00001110

PRINT numeric_value1 AND numeric_value2

END
' Using the AND operator on two conditional expressions

numeric_value1 = 15

numeric_value2 = 25

IF numeric_value1 > 10 AND numeric_value1 < 20 THEN PRINT "Numeric_Value1 is between 10 and 20"

IF numeric_value2 > 10 AND numeric_value2 < 20 THEN PRINT "Numeric_Value2 is between 10 and 20"

END

' This will output "Numeric_Value1 is between 10 and 20" because

' both conditions of the IF statement is true

' It will not output the result of the second IF statement because the first

' condition is true and the second is false.

Differences from other BASICs

  • none from Visual BASIC
  • PBASIC AND is always logical, and & is bitwise

See also