# Introduction to Bitwise Operation3 min read

## Bitwise Operations

### Binary Number

In mathematics and digital electronics, a binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically 0 and 1.

Computer memory is composed of bytes where each byte contains 8 bits. Typically, computer operations are at the byte level – creating variables, retrieving values, pointing to memory locations, and so on. However, for situations that require manipulation of individual bits in a byte, there are bitwise operations that set and read individual bits of a byte in memory.

##### Also read:

### What is a bitwise operation?

Bitwise operations fall into two categories. Logical operations and shift operations. Similar to the logical operators **AND**, **OR**, and **NOT**, there are bitwise operators AND, OR, Exclusive OR, and NOT. However, the bitwise logical operators compare individual bits rather than values comprised of bytes. The second type of bitwise operation, the **shift**, is not the result of an operation, but rather a movement of the bits in a byte to either the left or the right.

#### Bit Values

Computers are based on digital electronics, which has two states, ON and OFF. These states naturally correlate to 1 and 0, which can also be thought of as **TRUE** and **FALSE**.

Since computers operate digitally, there is a need for a way to represent numbers, letters, and so on with just 1s and 0s. For this, we use the binary number system which consists of just two digits, 1 and 0.

Consider the decimal number 6. Converted to binary, 6 becomes 110 (read as one one zero), we will learn how to convert decimal number to binary number on the later article. With this representation, the decimal number 6 is the first three bits of a byte set to 110.

Similarly, a character is simply an integer value that has been assigned a keyboard character or other special symbol using ASCII (American Standard Code for Information Interchange).

#### The Bitwise Operators

Most modern programming languages have the following bitwise operators (although the symbols might vary from language to language):

- & bitwise
**AND**`(a&b)`

: Returns a`1`

in each bit position for which the corresponding bits of both operands are`1`

s. - | bitwise
**OR**`(a|b)`

: Returns a`1`

in each bit position for which the corresponding bits of either or both operands are`1`

s. - ^ bitwise Exclusive OR (
**XOR**)`(a^b)`

: Returns a`1`

in each bit position for which the corresponding bits of either but not both operands are`1`

s. - ~ bitwise Complement (
**NOT**)`~ a`

: Inverts the bits of its operand. - >> bitwise Left Shift
`(a<<b)`

: Shifts`a`

in binary representation`b`

(< 32) bits to the left, shifting in`0`

s from the right. - << bitwise Right Shift
`(a>>b)`

: Shifts`a`

in binary representation`b`

(< 32) bits to the right, discarding bits shifted off. - >> zero-filled Right Shift
`(a>>>b)`

: Shifts`a`

in binary representation`b`

(< 32) bits to the right, discarding bits shifted off, and shifting in`0`

s from the left.

Read the next articles:

- Learn how to convert decimal, hexadecimal number to binary number and vice versa.
- Bitwise manipulation: AND, OR, NOT

## Leave a Reply