Browse Definitions :
Definition

# bitwise

## What is bitwise?

Bitwise is a level of operation that involves working with individual bits which are the smallest units of data in a computing system. Each bit has single binary value of 0 or 1. Most programming languages manipulate groups of 8, 16 or 32 bits. These bit multiples are known as bytes.

The arithmetic logic unit (ALU) is a part of a computer's CPU. Inside the ALU, mathematical operations like addition, subtraction, multiplication and division are all done at bit level. For those operations, bitwise operators are used.

## Bitwise operations

A bitwise operation operates on two-bit patterns of equal lengths by positionally matching their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both the first AND second bits are 1. If only one bit is a 1, the result is 0. AND can also be used to test individual bits in a bit string to see if they are 0 or 1.

A logical OR (|) operation functions differently from the AND operations. For each bit pair, the result is 1 if the first OR second bit is 1. If neither bit is 1, the result is 0.

A logical XOR (~) of each bit pair results in a 1 if the two bits are different, and 0 if they are the same (both zeros or both ones).

Logical NOT is represented as ^.

Left shift (<<), right shift (>>) and zero-fill right shift (>>>) bitwise operators are also known as bit shift operators.

## Bitwise operators

Bitwise operators are characters that represent actions (bitwise operations) to be performed on single bits. They operate at the binary level and perform operations on bit patterns that involve the manipulation of individual bits. Thus, unlike common logical operators like + or - which work with bytes or groups of bytes, bitwise operators can check each individual bit within a byte.

The most common bitwise operators used in C/C++ are given in the table below.

 Operator Name Description Application & Bitwise AND Copies a bit to the result if it exists in both operands. The result is 1 only if both bits are 1. To set up a mask to check the values of specific bits | Bitwise OR Copies a bit to the result if it exists in either operand. The result is 1 if either bit is 1. To add two numbers if there is no carry involved ^ Bitwise Exclusive OR (XOR) Copies a bit to the result if it exists in either operand. So, if one of the operands is TRUE, the result is TRUE. If neither operand is TRUE, the result is FALSE. To toggle bits or swap two variables without using a third temporary variable To find specific types of numbers (e.g., odd) in a series of numbers (e.g., all even) To find nonrepeating elements To detect if two integers have opposite signs ~ Bitwise NOT Also known as bitwise complement and bitwise inversion, it flips zeros into ones and ones into zeros. To flip or invert bits << Shift left The left operand value is shifted left by the number of bits specified by the right operand. To align bits >> Shift right The left operand value is shifted right by the number of bits specified by the right operand. To align bits

Multiple bitwise operators are used in bit manipulation. These operations happen very fast and optimize system performance and time complexity.

It's important to keep in mind that the left shift and right shift operators should not be used for negative numbers. Doing this can result in undefined behaviors in the programming language.

Also, bitwise operators should not be used in place of logical operators because they work differently. Logical operators consider non-zero operands as 1 and their result is either 0 or 1. In contrast, bitwise operators return an integer value.

The table below defines the JavaScript bitwise operators.

 Operator Name Type Action & Bitwise AND Binary If bits of both operands are ones, returns a one in each bit position | Bitwise OR Binary If bits of either operand are ones, returns a one in a bit position ^ Bitwise XOR Binary If a single operand is a one, returns a one in a bit position ~ Bitwise NOT Unary Flips the bits in the operand << Left shift Binary Shifts first operand a number of bits to the left as specified in the second operand, shifting in zeros from the right >> Right shift Binary Shifts first operand a number of bits to the right as specified in the second operand, and discards displaced bits >>> Zero-fill right shift Binary Shifts first operand a number of bits to the right as specified in the second operand, discards displaced bits, and shifts in zeros from the left

## Applications of bitwise operations and operators

There are many applications of bitwise operations and operators. For one, they are used in data compression where data is converted from one representation to another to reduce the amount of storage space required. Bitwise operations are also used in encryption algorithms to encrypt data and protect it from unauthorized use, manipulation or exfiltration.

The following are some other common applications:

• low-level programming for device drivers, memory allocators and compression software;
• maintaining large integer sets for search and optimization;
• ability to store multiple Boolean flags on limited memory devices;
• embedded software in chips and microcontrollers;
• communications where individual header bits carry sensitive or important information; and
• converting text cases, such as uppercase to lowercase or lowercase to uppercase.

## Bitwise AND

The bitwise AND operator produces an output of 1 if the corresponding bits of both the operands are 1. If not, the output is 0.

Example 1: Bitwise AND operation of two one-bit operands.

 Left operand Right operand Result 0 0 0 0 1 0 1 0 0 1 1 1

Example 2: Bitwise AND operation of two integers: 28 and 17; the & operator compares each binary digit of these integers.

 Binary digits 28 0 0 0 1 1 1 0 0 17 0 0 0 1 0 0 0 1 Are both digits 1? No No No Yes No No No No Bitwise AND output 0 0 0 1 0 0 0 0

Thus: 28 & 17 (bitwise AND) = 00010000 (binary) = 16 (decimal).

## Bitwise OR

The bitwise OR operator produces an output of 1 if either one of the corresponding bits is 1. Otherwise, the output is zero.

Example 1: The bitwise OR operation of two one-bit operands.

 Left operand Right operand Result 0 0 0 0 1 1 1 0 1 1 1 1

Example 2: Let's consider the previous example of two integers: 28 and 17.

 Binary digits 28 0 0 0 1 1 1 0 0 17 0 0 0 1 0 0 0 1 Is either digit 1? No No No Yes Yes Yes No Yes Bitwise OR output 0 0 0 1 1 1 0 1

Thus: 28 | 17 (bitwise OR) = 00011101 (binary) = 29 (decimal).

## Bitwise exclusive OR (XOR)

The bitwise exclusive OR (XOR) operator returns 1 if the bits of both operands are opposite. Otherwise, it returns 0.

Example 1: The bitwise XOR operation of two one-bit operands.

 Left operand Right operand Result 0 0 0 0 1 1 1 0 1 1 1 0

Example 2: Let's see how bitwise XOR works for our two integers 28 and 17.

 Binary digits 28 0 0 0 1 1 1 0 0 17 0 0 0 1 0 0 0 1 Are the two digits opposite of each other? No No No No Yes Yes No Yes Bitwise XOR output 0 0 0 0 1 1 0 1

Thus: 28 ^ 17 (bitwise XOR) = 00001101 (binary) = 13 (decimal).

## Bitwise NOT

The bitwise NOT operator reverses the bits. Unlike other bitwise operators, it accepts only one operand.

Example: Let's consider the bitwise NOT operation of the integer 28.

 Binary digits 28 0 0 0 1 1 1 0 0 Bitwise NOT output 1 1 1 0 0 0 1 1

Thus: ~28 (bitwise NOT) = 11100011 (binary) = 227 (decimal).

## Bitwise left shift

The bitwise left shift operator shifts the bits left by the bits specified by the right operand. The positions vacated by the left shift operator are filled with 0.

Example: Let's perform the bitwise left shift operation on the integer 6. Each bit will be shifted left by 1.

6 = 0110

6<<1 = 1100 (binary) = 12 (decimal)

## Bitwise right shift

Like the left shift operator, the bitwise right shift operator shifts the bits right by the bits specified by the right operand. The positions vacated by the right shift operator are filled with 0.

Example: Let's perform the right shift by two bits operations on the integer 8. Each bit will be shifted right by 2.

8 = 1000

8>>2 = 0010 (binary) = 2 (decimal)

See also: bit stuffing, logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR), How many bytes for..., classical computing, Advanced Business Application Programming

This was last updated in July 2022

#### Continue Reading About bitwise

• subnet (subnetwork)

A subnet, or subnetwork, is a segmented piece of a larger network. More specifically, subnets are a logical partition of an IP ...

• secure access service edge (SASE)

Secure access service edge (SASE), pronounced sassy, is a cloud architecture model that bundles together network and cloud-native...

• Transmission Control Protocol (TCP)

Transmission Control Protocol (TCP) is a standard protocol on the internet that ensures the reliable transmission of data between...

• personally identifiable information (PII)

Personally identifiable information (PII) is any data that could potentially identify a specific individual.

• zero-day vulnerability

A zero-day vulnerability is a security loophole in software, hardware or firmware that threat actors exploit before the vendors ...

• DNS attack

A DNS attack is an exploit in which an attacker takes advantage of vulnerabilities in the domain name system.

• data collection

Data collection is the process of gathering data for use in business decision-making, strategic planning, research and other ...

• chief trust officer

A chief trust officer (CTrO) in the IT industry is an executive job title given to the person responsible for building confidence...

• green IT (green information technology)

Green IT (green information technology) is the practice of creating and using environmentally sustainable computing resources.

• diversity, equity and inclusion (DEI)

Diversity, equity and inclusion is a term used to describe policies and programs that promote the representation and ...

• ADP Mobile Solutions

ADP Mobile Solutions is a self-service mobile app that enables employees to access work records such as pay, schedules, timecards...

• director of employee engagement

Director of employee engagement is one of the job titles for a human resources (HR) manager who is responsible for an ...

• digital marketing

Digital marketing is the promotion and marketing of goods and services to consumers through digital channels and electronic ...

• contact center schedule adherence

Contact center schedule adherence is a standard metric used in business contact centers to determine whether contact center ...

• customer retention

Customer retention is a metric that measures customer loyalty, or an organization's ability to retain customers over time.

Close