### Table of Contents

# 6.04 Binary & Logical Operations

## Binary Operators

Binary operations are used to test and set individual bits and bit combinations. They are most often used when dealing with internal representation on computers.

**Number Formats**- &h or 0x
- Prefix for hexadecimal constant (0x1F, &h3C)
- &o or 0o
- Prefix for octal constant (0o33, &o33)
- &b or 0b
- Prefix for binary constant (0b1010, &b1110)

The following operators are used. They are listed in priority order. Those that are in a group that is higher in the list take precedence over those in a group that is lower down the list.

**Operator****Usage****Binary Operations**- <<, >>
- bitwise shift left and shift right.

0b0011 << 1 → 0b0110

0b1100 >> 3 → 0b0001 - ~
- bitwise NOT (~A).

Result bit is 1 if operand A bit is 0 and vice versa, otherwise result bit is 0.

~0b1100 → 0b0011 - BAND or &
- bitwise AND (A AND B)

Result bit is 1 if both operand bits are 1, otherwise result bit is 0.

0b1100 & 0b1010 → 0b1000 - BOR or |
- bitwise OR (A OR B)

Result bit is 1 if either operand bit is 1, otherwise result bit is 0.

0b1100 | 0b1010 → 0b1110 - XOR
- bitwise XOR (A XOR B).

Result bit is 1 if either A is 1 or B is 1 but not both(i.e. not the same), otherwise result bit is 0.

0b1100 XOR 0b1010 → 0b0110 - EQV

XNOR - bitwise EQV/XNOR (NOT (A XOR B)).

Result bit is 1 if both operand bits are the same, otherwise result bit is 0.

0b1100 EQV 0b1010 → 0b1001 - IMP
- bitwise IMP (NOT (A AND NOT B)).

Result bit is 0 if first operand bit is 1 and second operand bit is 0, otherwise result bit is 1.

0b1100 IMP 0b1010 → 0b1011 - NAND
- bitwise NAND (NOT (A AND B))

Result bit is 0 if both operand bits are 1, otherwise result bit is 0.

0b1100 NAND 0b1010 → 0b0111 - NOR
- bitwise NOR (NOT (A OR B))

Result bit is 1 if both operand bits are 0, otherwise result bit is 1.

0b1100 NOR 0b1010 → 0b0001

## Logical Operators

Logical operations are used to test conditions to determine what action a program should take.

**Operator****Usage**- NOT or !
- Logical NOT (NOT false = true)
- =
- Equal
- <> or !=
- Not Equal
- <, >
- Less Than, Greater Than
- =<, =>, <=, >=
- Less Than or Equal, Greater than or Equal
- IN
- belongs to … (see The operator IN)
- LIKE
- Regular expression match (see The operator LIKE)
- AND or &&
- Logical AND (A AND B)
- OR or ||
- Logical OR (A OR B)