Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Bitwise XOR Operator in Programming
Next article icon

Bitwise AND operator in Programming

Last Updated : 12 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we'll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization techniques, and conclude with its significance in programming.

Table of Content

  • What is Bitwise AND?
  • Bitwise AND Operator
  • Bitwise AND Operator Syntax
  • Bitwise AND in C/C++
  • Bitwise AND in Java
  • Bitwise AND in Python
  • Bitwise AND in JavaScript
  • Bitwise AND in C#
  • Properties of Bitwise AND Operator
  • Applications of Bitwise AND Operator

What is Bitwise AND?

The Bitwise AND operation (&) is a binary operation that operates on each bit of its operands independently. It returns a new value where each bit of the result is determined by applying the AND operation to the corresponding bits of the operands.

The truth table for the Bitwise AND operation is as follows:

A

B

A AND B

0

0

0

0

1

0

1

0

0

1

1

1

In this table, A and B are the variables, and A AND B is the expression representing the logical AND operation. The table shows all possible combinations of truth values for A and B, and the resulting truth value of A AND B for each combination.

The AND operation returns 1 (true) if the both the inputs are 1, and 0 (false) otherwise. This is why the output is 1 for the fourth row, where A and B have values = 1, and 0 for the first, second and third rows, where at least one of A or B has value = 0.

Bitwise AND Operator:

The Bitwise AND operator, represented by the symbol &, is used to perform a logical AND operation on corresponding bits of two integers. It compares each bit of the first operand with the corresponding bit of the second operand and yields a result where a bit is set only if both corresponding bits are set in the operands. Both the operands should be of integral types.

Bitwise AND Operator Syntax:

The Bitwise AND operator (&) is a fundamental component of bitwise operations in programming languages. It operates on individual bits of two integers, comparing each corresponding bit and producing a result based on whether both bits are set or not.

result = operand1 & operand2;

Below, we'll explore the syntax of the Bitwise AND operator in various programming languages:

Bitwise AND in C/C++:

In C and C++, the syntax of the Bitwise AND operator is straightforward:

result = operand1 & operand2;

Here, operand1 and operand2 are the two integers on which the bitwise AND operation is performed. The result is stored in the variable result.

C++
#include <iostream> using namespace std;  int main() {     // declare variables     int A = 12, B = 25;      cout << "A & B = " << (A & B) << endl;      return 0; } 
C
#include <stdio.h>  int main() {      int A = 12, B = 25;     printf("A & B = %d", A & B);      return 0; } 

Output
A & B = 8

Bitwise AND in Java:

In Java, the Bitwise AND operator is represented by the symbol &:

result = operand1 & operand2; 
Java
// Java program to illustrate // bitwise operators  public class operators {     public static void main(String[] args)     {         // Initial values         int A = 12;         int B = 25;          // bitwise and         // 0101 & 0111=0101 = 5         System.out.println("A & B = " + (A & B));               } } 

Output
A & B = 8

Similar to C and C++, operand1 and operand2 are the two operands, and the result is stored in the variable result.

Bitwise AND in Python:

Python also supports bitwise operations, including the Bitwise AND operator, represented by the symbol &:

result = operand1 & operand2 
Python3
# Python program to show # bitwise operators  A = 12 B = 25  # Print bitwise AND operation print("A & B =", A & B) 

Output
A & B = 8

Similarly, operand1 and operand2 are the two operands, and the result is assigned to the variable result.

Bitwise AND in JavaScript:

In JavaScript, the Bitwise AND operator is also represented by the symbol &:

result = operand1 & operand2; 
Javascript
let A = 12;  let B = 25;   console.log("A & B = " + (A & B));  

Output
A & B = 8

Again, operand1 and operand2 are the operands, and the result is stored in the variable result.

Bitwise AND in C#:

In C#, the Bitwise AND operator is similar to other languages, represented by the symbol &:

result = operand1 & operand2; 
C#
using System;  class Program {     static void Main(string[] args)     {         // declare variables         int A = 12, B = 25;         Console.WriteLine("A & B = " + (A & B));      } } 

Output
A & B = 8

Once again, operand1 and operand2 are the operands, and the result is assigned to the variable result.

Properties of Bitwise AND Operator:

  • Commutativity: The order of operands does not affect the result of the bitwise AND operation. In other words, a & b yields the same result as b & a.
  • Identity: Performing a bitwise AND operation with zero (0) always results in zero. That is, a & 0 = 0 for any value of a.
  • Idempotence: Performing a bitwise AND operation with itself results in the same value. That is, a & a = a.

Applications of Bitwise AND Operator:

  • Masking: Bitwise AND is commonly used in masking operations to extract specific bits or flags from a bit pattern.
  • Checking Parity of a number: By performing a bitwise AND operation with 1, you can determine whether a number is even or odd. An even number & 1 results in 0, while an odd number & 1 results in 1.
  • Clearing Bits: To clear specific bits in an integer, you can use the bitwise AND operator with a mask where the bits you want to clear are set to 0.

Conclusion:

The Bitwise AND operator plays a crucial role in low-level programming, enabling efficient manipulation of individual bits within integers. Understanding its properties, syntax, and applications can significantly enhance your ability to write optimized and efficient code, especially in scenarios where performance is critical. By leveraging the bitwise AND operator effectively, programmers can unlock powerful capabilities for bitwise manipulation and optimization in their programs.


Next Article
Bitwise XOR Operator in Programming

J

jyotijb23
Improve
Article Tags :
  • Programming

Similar Reads

  • Bitwise OR Operator (|) in Programming
    In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise OR operator (|). In this article, we’ll discuss the Bitwise OR operator, its syntax, properties, applications, and optimization techniques, and concl
    5 min read
  • Bitwise XOR Operator in Programming
    Bitwise XOR Operator is represented by the caret symbol (^). It is used to perform a bitwise XOR operation on the individual bits of two operands. The XOR operator returns 1 if the corresponding bits in the two operands are different, and 0 if they are the same. Table of Content What is Bitwise XOR?
    5 min read
  • Conditional Operator in Programming
    Conditional Operator, often referred to as the ternary operator, is a concise way to express a conditional (if-else) statement in many programming languages. It is represented by the "?" symbol and is sometimes called the ternary operator because it takes three operands. Table of Content Syntax of C
    9 min read
  • Logical AND operator in Programming
    In programming, Logical operators play a crucial role in manipulating individual data. One of the fundamental logical operators is the Logical AND operator(&&).In this article, we’ll discuss what is a Logical AND operator, its syntax, properties, applications, and optimization techniques, an
    5 min read
  • Binary Operators in Programming
    Binary Operators are essential tools in programming that perform operations on pairs of data, enabling tasks like calculations, comparisons, logical operations, and bitwise manipulations. They are fundamental for processing and manipulating data efficiently in computer programs. Table of Content Wha
    14 min read
  • Operator Associativity in Programming
    Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in progr
    14 min read
  • Assignment Operators in Programming
    Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin
    7 min read
  • Comparison Operators in Programming
    Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional
    10 min read
  • What are Operators in Programming?
    Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Table of Content What are Operato
    15+ min read
  • Bitwise Left Shift(<<) Operator in Programming
    Bitwise operators play a significant role in manipulating individual bits within binary data. Among these operators, the Bitwise Left Shift (<<) operator is used for shifting the bits of a number to the left by a specified number of positions. In this blog post, we'll explore the definition, s
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences