Table of Contents
Understanding the bitwise operators
Common use cases and techniques
1. Checking if a number is odd or even
2. Toggling a specific bit
3. Setting or clearing a bit
4. Extracting a bit
5. Swapping two numbers without extra space
6. Counting set bits (population count)
Important considerations
Practical example: Managing flags
Home Backend Development C++ How to work with bitwise operators in C

How to work with bitwise operators in C

Aug 13, 2025 am 12:25 AM
c++ Bit operators

Bitwise operators in C++ are essential for manipulating individual bits in data, providing efficient control in low-level programming, embedded systems, and algorithmic optimizations. The six main operators are: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift), all operating on integral types at the bit level. For example, with unsigned char a = 5 (00000101) and b = 3 (00000011), a & b yields 1 (00000001), a | b gives 7 (00000111), a ^ b results in 6 (00000110), ~a produces 250 (11111010 in 8-bit), a << 1 equals 10 (multiplication by 2), and a >> 1 equals 2 (division by 2). Common techniques include checking if a number is odd or even using n & 1, toggling the k-th bit with n ^= (1 << k), setting the k-th bit via n |= (1 << k), clearing it with n &= ~(1 << k), and extracting a bit using (n >> k) & 1. XOR can swap two values without a temporary variable: a ^= b; b ^= a; a ^= b;, though this may affect code clarity. Counting set bits can be done manually with a loop or using GCC’s __builtin_popcount(n). Important considerations include using unsigned types to ensure predictable right-shift behavior, being aware of integer promotion and sign extension, and respecting operator precedence—parentheses are crucial, as in (flags & MASK) == VALUE to avoid errors due to == having higher precedence than &. Shifting by negative amounts or beyond bit width causes undefined behavior. A practical application is managing flags: constants like READ = 1, WRITE = 2, EXECUTE = 4 allow combining permissions with |, checking with &, and removing with ~, a pattern common in system programming. In summary, bitwise operators offer fast, precise bit-level control when used carefully with proper type selection, parentheses, and clear documentation, making them powerful tools in performance-critical and hardware-related code.

How to work with bitwise operators in C++

Working with bitwise operators in C++ is essential when you need to manipulate individual bits in data, such as in low-level programming, embedded systems, optimization, or solving algorithmic problems. These operators work directly on the binary representation of integers. Here’s how to use them effectively.

Understanding the bitwise operators

C++ provides six main bitwise operators:

  • & (AND) – Returns 1 if both bits are 1.
  • | (OR) – Returns 1 if at least one bit is 1.
  • ^ (XOR) – Returns 1 if the bits are different.
  • ~ (NOT) – Inverts all bits (1 becomes 0, 0 becomes 1).
  • << (Left shift) – Shifts bits to the left, multiplying by powers of 2.
  • >> (Right shift) – Shifts bits to the right, dividing by powers of 2 (arithmetic shift for signed, logical for unsigned).

These operators work on integral types (int, char, long, etc.) and are evaluated bit by bit.

For example:

unsigned char a = 5;  // 00000101
unsigned char b = 3;  // 00000011

cout << (a & b) << endl;  // 1 → 00000001
cout << (a | b) << endl;  // 7 → 00000111
cout << (a ^ b) << endl;  // 6 → 00000110
cout << (~a) << endl;     // 250 (assuming 8-bit) → 11111010
cout << (a << 1) << endl; // 10 → 00001010 (5 * 2)
cout << (a >> 1) << endl; // 2 → 00000010 (5 / 2)

Common use cases and techniques

1. Checking if a number is odd or even

Use the AND operator with 1 to check the least significant bit (LSB).

if (n & 1) {
    cout << "Odd" << endl;
} else {
    cout << "Even" << endl;
}

2. Toggling a specific bit

Use XOR to flip a bit at a given position.

n ^= (1 << k);  // Toggles the k-th bit (0-indexed)

3. Setting or clearing a bit

  • Set the k-th bit: n |= (1 << k)
  • Clear the k-th bit: n &= ~(1 << k)

This is useful in flags or configuration registers.

4. Extracting a bit

Check if the k-th bit is set:

bool isSet = (n >> k) & 1;

5. Swapping two numbers without extra space

Though not always recommended due to readability, XOR can swap values:

a ^= b;
b ^= a;
a ^= b;

Or more concisely:

a ^= b ^= a ^= b;  // Be cautious with side effects and sequence points

6. Counting set bits (population count)

Manually loop through bits:

int count = 0;
while (n) {
    count += n & 1;
    n >>= 1;
}

Or use built-in functions like __builtin_popcount(n) in GCC.

Important considerations

  • Signed vs unsigned: Right shifting signed negative numbers is implementation-defined (usually arithmetic shift). Use unsigned types for predictable behavior.
  • Integer promotion: Smaller types (like char, short) are promoted to int during operations. Be aware of sign extension.
  • Operator precedence: Bitwise operators have lower precedence than comparison and arithmetic operators. Use parentheses to avoid bugs.
    if ((flags & MASK) == VALUE)  // Correct
    if (flags & MASK == VALUE)    // Wrong! == has higher precedence
  • Avoid undefined behavior: Shifting by negative counts or by more than the bit width is undefined.
    x << n;  // Undefined if n < 0 or n >= bit-width of x

Practical example: Managing flags

Bitwise operators are great for managing multiple boolean options in a single variable.

const int READ = 1 << 0;    // 1
const int WRITE = 1 << 1;   // 2
const int EXECUTE = 1 << 2; // 4

int permissions = 0;
permissions |= READ | WRITE;        // Grant read and write
if (permissions & EXECUTE) {        // Check execute
    cout << "Executable" << endl;
}
permissions &= ~WRITE;              // Remove write

This pattern is widely used in system programming and APIs.

Basically, bitwise operators give you fine control over data at the bit level. They’re fast, efficient, and once you get used to binary thinking, quite intuitive. Just remember to use parentheses, prefer unsigned types, and document your bit logic clearly.

The above is the detailed content of How to work with bitwise operators in C. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1596
276
What should I do if the application cannot start normally (0xc0000906)? See the solution here What should I do if the application cannot start normally (0xc0000906)? See the solution here Aug 13, 2025 pm 06:42 PM

When opening the software or game, a prompt suddenly appears that "the application cannot start normally (0xc0000906)" appears, and many users will be confused and don't know where to start. In fact, most of these errors are caused by corruption of system files or missing runtime libraries. Don't rush to reinstall the system. This article provides you with several simple and effective solutions to help you quickly restore the program to run. 1. What is the error of 0xc0000906? Error code 0xc0000906 is a common startup exception in Windows systems, which usually means that the program cannot load the necessary system components or running environment when running. This problem often occurs when running large software or games. The main reasons may include: the necessary runtime library is not installed or damaged. The software installation package is endless

How to fix missing MSVCP71.dll in your computer? There are only three methods required How to fix missing MSVCP71.dll in your computer? There are only three methods required Aug 14, 2025 pm 08:03 PM

The computer prompts "MsVCP71.dll is missing from the computer", which is usually because the system lacks critical running components, which causes the software to not load normally. This article will deeply analyze the functions of the file and the root cause of the error, and provide three efficient solutions to help you quickly restore the program to run. 1. What is MSVCP71.dll? MSVCP71.dll belongs to the core runtime library file of Microsoft VisualC 2003 and belongs to the dynamic link library (DLL) type. It is mainly used to support programs written in C to call standard functions, STL templates and basic data processing modules. Many applications and classic games developed in the early 2000s rely on this file to run. Once the file is missing or corrupted,

How to use regular expressions in C How to use regular expressions in C Aug 12, 2025 am 10:46 AM

To use regular expressions in C, you need to include header files and use the functions it provides for pattern matching and text processing. 1. Use std::regex_match to match the full string, and return true only when the entire string conforms to the pattern; 2. Use std::regex_search to find matches at any position in the string; 3. Use std::smatch to extract the capture group, obtain the complete match through matches[0], matches[1] and subsequent sub-matches; 4. Use std::regex_replace to replace the matching text, and support the capture group with references such as $1 and $2; 5. You can add an iset when constructing the regex (

C   operator overloading example C operator overloading example Aug 15, 2025 am 10:18 AM

Operator overloading in C allows new behaviors of standard operators to be assigned to custom types, 1. Return new objects through member function overloading; 2. Overload = Modify the current object and return reference; 3. Friend function overloading

std::map vs std::unordered_map in C std::map vs std::unordered_map in C Aug 14, 2025 pm 06:53 PM

In C, the choice of std::map and std::unordered_map depends on the specific requirements. 1. Different underlying structures: std::map is implemented based on red and black trees, with keys stored in order, default ascending order, and the complexity of search and insertion is O(logn); std::unordered_map uses a hash table, unordered, and the average complexity of search and insertion is O(1), and the worst is O(n). 2. Insertion performance and memory overhead: map insertion requires maintenance of tree structure and is less efficient; unordered_map insertion is faster but consumes more memory, and can be optimized through reserve(). 3. Custom comparison function: map supports custom comparison function, unordered

C   vector of strings example C vector of strings example Aug 21, 2025 am 04:02 AM

The basic usage of std::vector includes: 1. Declare vector; 2. Add elements with push_back(); 3. Initialize with initialization list; 4. Loop traversal with range for; 5. Access elements through index or back(); 6. Direct assignment of values to modify elements; 7. Delete the end elements with pop_back(); 8. Call size() to get the number of elements; it is recommended to use constauto& to avoid copying, pre-allocate reserve() to improve performance, and pay attention to checking that it is not empty before access. This data structure is an efficient and preferred way to handle string lists.

How to work with std::variant in C How to work with std::variant in C Aug 14, 2025 am 11:32 AM

std::variant is a type-safe union introduced by C 17. It can safely hold the value of one of the specified types. It can realize secure access and type checking through methods such as std::get, std::holds_alternative, std::visit and std::get_if. Combined with std::monostate, optional values can be simulated. It is recommended to use std::visit for type distribution and avoid large type lists to improve maintainability, and ultimately ensure type safety and exception safety.

How to write a basic Makefile for a C   project? How to write a basic Makefile for a C project? Aug 15, 2025 am 11:17 AM

AbasicMakefileautomatesC compilationbydefiningruleswithtargets,dependencies,andcommands.2.KeycomponentsincludevariableslikeCXX,CXXFLAGS,TARGET,SRCS,andOBJStosimplifyconfiguration.3.Apatternrule(%.o:%.cpp)compilessourcefilesintoobjectfilesusing$

See all articles