Explain how the bitwise AND operator (&) works and provide an example.

Quick Overview

Explain how the bitwise AND operator combines two integers bit by bit and work through an example in binary. Tests binary arithmetic, using masks to test and isolate bits, and the difference between bitwise AND and short-circuit logical AND.

Explain how the bitwise AND operator (&) works and provide an example.

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Explain how the bitwise AND operator (`&`) works and provide an example. AND is the usual way to test or isolate bits with a mask, so be ready to show how it is used as well as what it computes. ```hint Look at each column Write both numbers in binary, one above the other, and decide each result bit from the two bits in its column alone. ``` ### What a Strong Answer Covers - The rule AND applies at each bit position, in words or as a truth table. - A worked example showing both operands and the result in binary. - How AND is used with a mask to test, keep or clear bits. - How bitwise `&` differs from logical `&&` or Python's `and`. - A common pitfall, such as operator precedence in C or Java. ### Follow-up Questions - How can you check with one AND whether a positive integer is a power of two? - Why does `x & (m - 1)` equal `x % m` when m is a power of two and x is nonnegative, and where is that used? - What does `x & -x` return, and what is it used for?

Overview: Explain how the bitwise AND operator combines two integers bit by bit and work through an example in binary. Tests binary arithmetic, using masks to test and isolate bits, and the difference between bitwise AND and short-circuit logical AND.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Explain how the bitwise AND operator (&) works and provide an example.

AND is the usual way to test or isolate bits with a mask, so be ready to show how it is used as well as what it computes.

What a Strong Answer Covers Guidance

  • The rule AND applies at each bit position, in words or as a truth table.
  • A worked example showing both operands and the result in binary.
  • How AND is used with a mask to test, keep or clear bits.
  • How bitwise & differs from logical && or Python's and .
  • A common pitfall, such as operator precedence in C or Java.

Follow-up Questions Guidance

  • How can you check with one AND whether a positive integer is a power of two?
  • Why does x & (m - 1) equal x % m when m is a power of two and x is nonnegative, and where is that used?
  • What does x & -x return, and what is it used for?