- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Bitwise Operators
![]() Share with a Friend |
C Programming - C Bitwise Operators
C Bitwise Operators
Bitwise operators in C perform operations directly on the binary representations of integers. These operators manipulate individual bits of data, making them highly efficient for low-level programming, such as embedded systems, hardware interfacing, and performance-critical applications.
List of Bitwise Operators
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
|
& |
Bitwise AND |
Performs AND operation on each bit. |
a & b |
0100 (binary) |
|
` |
` |
Bitwise OR |
Performs OR operation on each bit. |
`a |
|
^ |
Bitwise XOR |
Performs XOR (exclusive OR) operation on each bit. |
a ^ b |
1010 (binary) |
|
~ |
Bitwise NOT (One's Complement) |
Flips all bits (1 to 0, and 0 to 1). |
~a |
1011 (binary) |
|
<< |
Left Shift |
Shifts bits to the left by a specified number of positions. |
a << 2 |
1100 (binary) |
|
>> |
Right Shift |
Shifts bits to the right by a specified number of positions. |
a >> 2 |
0011 (binary) |
Key Characteristics
- Bitwise AND (&):
- Compares each bit of the operands and sets the corresponding bit in the result to 1 only if both bits are 1.
C
int a = 12, b = 10; // a = 1100, b = 1010
int result = a & b; // result = 1000 (8 in decimal)
- Bitwise OR (|):
- Compares each bit of the operands and sets the corresponding bit in the result to 1 if at least one bit is 1.
C
int a = 12, b = 10; // a = 1100, b = 1010
int result = a | b; // result = 1110 (14 in decimal)
- Bitwise XOR (^):
- Compares each bit of the operands and sets the corresponding bit in the result to 1 if the bits are different.
C
int a = 12, b = 10; // a = 1100, b = 1010
int result = a ^ b; // result = 0110 (6 in decimal)
- Bitwise NOT (~):
- Inverts all bits (flips 1 to 0 and 0 to 1).
C
int a = 12; // a = 1100
int result = ~a; // result = 0011 (in 2's complement: -13 in decimal)
- Left Shift (<<):
- Shifts the bits of the operand to the left by the specified number of positions. The vacant bits on the right are filled with 0.
C
int a = 3; // a = 0011
int result = a << 2; // result = 1100 (12 in decimal)
- Right Shift (>>):
- Shifts the bits of the operand to the right by the specified number of positions. The vacant bits on the left depend on the type of shift (logical or arithmetic).
C
int a = 12; // a = 1100
int result = a >> 2; // result = 0011 (3 in decimal)
Example Program
C
#include <stdio.h>
int main() {
int a = 12, b = 10;
printf("a & b = %d\n", a & b); // Bitwise AND
printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
printf("~a = %d\n", ~a); // Bitwise NOT
printf("a << 2 = %d\n", a << 2); // Left Shift
printf("a >> 2 = %d\n", a >> 2); // Right Shift
return 0;
}
Output:
a & b = 8
a | b = 14
a ^ b = 6
~a = -13
a << 2 = 48
a >> 2 = 3
Applications of Bitwise Operators
- Masking Bits:
- Used to extract specific bits from a binary number.
C
int num = 0b10101010;
int mask = 0b00001111;
int result = num & mask; // Extracts the lower 4 bits
- Setting/Clearing Specific Bits:
- Set a bit: num | (1 << n)
- Clear a bit: num & ~(1 << n)
C
int num = 0b10101010;
num |= (1 << 3); // Set the 4th bit
num &= ~(1 << 2); // Clear the 3rd bit
- Toggling Bits:
- Flip specific bits using XOR.
C
int num = 0b10101010;
num ^= (1 << 3); // Toggle the 4th bit
- Checking Specific Bits:
- Check if a bit is set: (num & (1 << n)) != 0
C
int num = 0b10101010;
if (num & (1 << 3)) {
printf("4th bit is set\n");
}
- Efficient Multiplication/Division:
- Multiply by 2^n: num << n
- Divide by 2^n: num >> n
C
int num = 5;
int result = num << 3; // Multiplies num by 8
Points to Remember
- Bitwise operations are performed on integers only.
- Be cautious with signed integers during shifts, as results may vary.
- Use parentheses for clarity in complex expressions involving bitwise operators.
- Use bitwise operators in scenarios requiring performance-critical optimizations.
