Bitwise AND Operator (&)

The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0.

Truth table of AND operator

Example: 

Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y

Bitwise ANDof (7 & 4)

Implementation of AND operator:

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{

    int a = 7, b = 4;
    int result = a & b;
    cout << result << endl;

    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main (String[] args) {
        int a = 7, b = 4;
          int result = a & b;
          System.out.println(result);
    }
}

// This code is contributed by lokeshmvs21.
Python
a = 7
b = 4
result = a & b
print(result)
# This code is contributed by akashish__
C#
using System;

public class GFG{

    static public void Main (){
      int a = 7, b = 4;
      int result = a & b;
      Console.WriteLine(result);
    }
}

// This code is contributed by akashish__
Javascript
let a = 7, b = 4;
let result = a & b;
console.log(result);
// This code is contributed by akashish__

Output
4

Time Complexity: O(1) 
Auxiliary Space: O(1)

Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial

Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.

Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial

Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.

Table of Content

  • What is Bitwise Algorithms?
  • Bitwise Operators / Basics of Bit manipulation
  • Bitwise AND Operator (&)
  • Bitwise OR Operator (|)
  • ​Bitwise XOR Operator (^)
  • Bitwise NOT Operator (!~)
  • Left-Shift (<<)
  • Right-Shift (>>)
  • Application of Bit Operators
  • Important Practice Problems on Bitwise Algorithm

Similar Reads

What is Bitwise Algorithms?

Bitwise algorithms refer to algorithms that perform operations on individual bits or bit patterns within computer data. These algorithms uses the binary representation of data and use the fundamental bitwise operations such as AND, OR, XOR, NOT, and bit shifting to manipulate and extract information from the data....

Bitwise Operators / Basics of Bit manipulation

An algorithmic operation known as bit manipulation involves the manipulation of bits at the bit level (bitwise). Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions....

Bitwise AND Operator (&)

The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0....

Bitwise OR Operator (|)

The | Operator takes two equivalent length bit designs as boundaries; if the two bits in the looked-at position are 0, the next bit is zero. If not, it is 1....

​Bitwise XOR Operator (^)

The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0....

Bitwise NOT Operator (!~)

All the above three bitwise operators are binary operators (i.e, requiring two operands in order to operate). Unlike other bitwise operators, this one requires only one operand to operate....

Left-Shift (<<)

The left shift operator is denoted by the double left arrow key (<<). The general syntax for left shift is shift-expression << k. The left-shift operator causes the bits in shift expression to be shifted to the left by the number of positions specified by k. The bit positions that the shift operation has vacated are zero-filled....

Right-Shift (>>)

The right shift operator is denoted by the double right arrow key (>>). The general syntax for the right shift is “shift-expression >> k”. The right-shift operator causes the bits in shift expression to be shifted to the right by the number of positions specified by k. For unsigned numbers, the bit positions that the shift operation has vacated are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used....

Application of Bit Operators

Bit operations are used for the optimization of embedded systems.The Exclusive-or operator can be used to confirm the integrity of a file, making sure it has not been corrupted, especially after it has been in transit.Bitwise operations are used in Data encryption and compression.Bits are used in the area of networking, framing the packets of numerous bits which are sent to another system generally through any type of serial interface.Digital Image Processors use bitwise operations to enhance image pixels and to extract different sections of a microscopic image....

Important Practice Problems on Bitwise Algorithm:

1. How to Set a bit in the number?...

Frequently Asked Questions (FAQs) on Bitwise Algorithms:

1. What are Bitwise Algorithms?...

Contact Us