- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Bitwise Operators in C
Bitwise operators in C allow low-level manipulation of data stored in computer’s memory.
Bitwise operators contrast with logical operators in C. For example, the logical AND operator (&&) performs AND operation on two Boolean expressions, while the bitwise AND operator (&) performs the AND operation on each corresponding bit of the two operands.
For the three logical operators &&, ||, and !, the corresponding bitwise operators in C are &, | and ~.
Additionally, the symbols ^ (XOR), << (left shift) and >> (right shift) are the other bitwise operators.
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) |
~ | Binary One's Complement Operator is unary and has the effect of 'flipping' bits. | (~A |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 |
Even though these operators work on individual bits, they need the operands in the form C data types or variables only, as a variable occupies a specific number of bytes in the memory.
Bitwise AND Operator (&) in C
The bitwise AND (&) operator performs as per the following truth table −
bit a | bit b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Bitwise binary AND performs logical operation on the bits in each position of a number in its binary form.
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the "a & b" operation results in 13, as per the bitwise ANDing of their corresponding bits illustrated below −
0011 1100 & 0000 1101 --------- = 0000 1100
The binary number 00001100 corresponds to 12 in decimal.
Bitwise OR (|) Operator
The bitwise OR (|) operator performs as per the following truth table −
bit a | bit b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Bitwise binary OR performs logical operation on the bits in each position of a number in its binary form.
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), then "a | b" results in 61, as per the bitwise OR of their corresponding bits illustrated below −
0011 1100 | 0000 1101 --------- = 0011 1101
The binary number 00111101 corresponds to 61 in decimal.
Bitwise XOR (^) Operator
The bitwise XOR (^) operator performs as per the following truth table −
bit a | bit b | a ^ b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Bitwise binary XOR performs logical operation on the bits in each position of a number in its binary form. The XOR operation is called "exclusive OR".
Note: The result of XOR is 1 if and only if one of the operands is 1. Unlike OR, if both bits are 1, XOR results in 0.
Assuming that the two int variables "a" and "b" have the values 60 (equivalent to 0011 1100 in binary) and 13 (equivalent to 0000 1101 in binary), the "a ^ b" operation results in 49, as per the bitwise XOR of their corresponding bits illustrated below −
0011 1100 ^ 0000 1101 --------- = 0011 0001
The binary number 00110001 corresponds to 49 in decimal.
The Left Shift (<<) Operator
The left shift operator is represented by the << symbol. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), the "a << 2" operation results in 240, as per the bitwise left-shift of its corresponding bits illustrated below −
0011 1100 << 2 = 1111 0000
The binary number 11110000 corresponds to 240 in decimal.
The Right Shift (>>) Operator
The right shift operator is represented by the >> symbol. It shifts each bit in its left-hand operand to the right by the number of positions indicated by the right-hand operand. Any blank spaces generated while shifting are filled up by zeroes.
Assuming that the int variable a has the value 60 (equivalent to 0011 1100 in binary), the "a >> 2" operation results in 15, as per the bitwise right-shift of its corresponding bits illustrated below −
0011 1100 >> 2 = 0000 1111
The binary number 00001111 corresponds to 15 in decimal.
The 1's Complement (~) Operator
The 1's compliment operator (~) in C is a unary operator, needing just one operand. It has the effect of "flipping" the bits, which means 1s are replaced by 0s and vice versa.
a | ~a |
---|---|
0 | 1 |
1 | 0 |
Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), then the "~a" operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits illustrated below −
~ 0011 1100 = 1100 0011
The binary number 1100 0011 corresponds to -61 in decimal.
Example
In this example, we have highlighted the operation of all the bitwise operators:
#include <stdio.h> int main(){ unsigned int a = 60; /* 60 = 0011 1100 */ unsigned int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ printf("Line 1 - Value of c is %d\n", c ); c = a | b; /* 61 = 0011 1101 */ printf("Line 2 - Value of c is %d\n", c ); c = a ^ b; /* 49 = 0011 0001 */ printf("Line 3 - Value of c is %d\n", c ); c = ~a; /*-61 = 1100 0011 */ printf("Line 4 - Value of c is %d\n", c ); c = a << 2; /* 240 = 1111 0000 */ printf("Line 5 - Value of c is %d\n", c ); c = a >> 2; /* 15 = 0000 1111 */ printf("Line 6 - Value of c is %d\n", c ); return 0; }
Output
When you run this code, it will produce the following output −
Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15