0% found this document useful (0 votes)
45 views31 pages

Bitwise Operations

The document outlines a course on Bitwise Operations for mechanical engineering students, detailing objectives such as understanding binary representation and applying bitwise operators like AND, OR, XOR, NOT, left shift, and right shift. It emphasizes the importance of these operations in interfacing with hardware and managing data efficiently in embedded systems. Additionally, it provides examples of how to use these operators in C programming, along with explanations of data types and their sizes.

Uploaded by

lamianayrin403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views31 pages

Bitwise Operations

The document outlines a course on Bitwise Operations for mechanical engineering students, detailing objectives such as understanding binary representation and applying bitwise operators like AND, OR, XOR, NOT, left shift, and right shift. It emphasizes the importance of these operations in interfacing with hardware and managing data efficiently in embedded systems. Additionally, it provides examples of how to use these operators in C programming, along with explanations of data types and their sizes.

Uploaded by

lamianayrin403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Computer Programming

Language

ME – 275
Credit: 3.00 Contact hour: 3.00

Course Teacher: Lecturer Md. Fahim Faisal


Contact Number: +8801521503917
Email: [email protected]
Topic : Bitwise
Operations

Class: 1+2+3
Date: 8 July 2025 (Tuesday)
Objectives:
o Recognize how integers and characters are represented in memory using binary.
o Visualize how bitwise operations manipulate individual bits.
o Develop intuition for bit patterns and binary math.
o & (AND)
o | (OR)
o ^ (XOR)
o ~ (NOT)
o << (Left shift)
o >> (Right shift)
o Understand and apply bit masking techniques.
o Efficiently store multiple Boolean flags using a single integer
o Interface with hardware in embedded/system-level code
Why Mechanical Engineers Should Learn Bitwise Operations

o Control sensors/actuators via microcontrollers

o Use in PLC and industrial automation

o Handle sensor data compactly in simulations

o Configure hardware using digital signals

o Diagnose faults using status registers

o Collaborate better in interdisciplinary teams

Bitwise operation include AND, OR, XOR, NOT


o Check bit
o Set bit
o Clear bit
o Toggle bit
RAM vs ROM

https://tse4.mm.bing.net/th/id/OIP.tGcABEoAmZIxtQIcJcgY1QHaHj?r=0&cb=thvnextc1&rs=1&pid=ImgDetMain&o=7&rm=3
https://images.minitool.com/partitionwizard.com/images/uploads/articles/2020/08/how-do-computers-work/how-do-computers-work-1.png
How values are stored?

Values are stored as bits in the memory.

For example, when we write int x = 5;

it is stored as 00000000 00000000 00000000 00000101

(in 32-bit system)

Our goal is to modify these bits directly to change the value of a variable using bitwise operators.

32 bit OS vs 64 bit OS
o Refers to how much data the CPU can process at once
o 32-bit → handles 2³² addresses (~4GB RAM max)
o 64-bit → handles 2⁶⁴ addresses (virtually unlimited RAM)
o 64-bit OS can run 64-bit and most 32-bit applications
o 32-bit OS cannot run 64-bit apps or use >4GB RAM
o 64-bit systems provide better performance, security, and multitasking

https://www.colocationamerica.com/wp-content/uploads/2015/12/bit-byte-comparison.jpg
How values are stored?

https://image4.slideserve.com/8933960/our-goal-is-to-modify-the-bits-l.jpg
https://i0.wp.com/samvssound.com/wp-content/uploads/2020/11/image-15.png?fit=429%2C327
Operators in C

https://miro.medium.com/v2/resize:fit:1400/1*7qp9atYnSmKKix_Al1w9iA.jpeg
Logic Gate

https://www.gatevidyalay.com/wp-content/uploads/2018/06/Logic-Gates-Types.png
https://miro.medium.com/v2/resize:fit:598/0*Jot3SmRSiLHkvx1X.gif
Bitwise Operators

https://iq.opengenus.org/content/images/2021/07/op-table-1.png
Bitwise Operator vs Logical Operator

Bitwise Operator Logical Operator


int a = 5; // 0101 int x = 5;
int b = 3; // 0011 int y = 3;

int c = a & b; // 0001 → 1 (bitwise AND) if (x && y) { // true, since both are non-
int d = a | b; // 0111 → 7 (bitwise OR) zero
int e = a ^ b; // 0110 → 6 (bitwise XOR) // executes
}

if (x || 0) { // true
}

if (!x) { // false, since x is non-


zero
// won’t execute
}
Data Type and Their Size in C
Data Type Typical Size (in Bytes) Description / Range (for 32-bit int)
-128 to 127 (signed), 0 to 255
char 1 byte (8 bits)
(unsigned)
int 4 bytes −2,147,483,648 to 2,147,483,647
short 2 bytes −32,768 to 32,767
long 4 bytes (on 32-bit) Same as int; 8 bytes on 64-bit Linux
long long 8 bytes −9 quintillion to 9 quintillion+
float 4 bytes ~6–7 decimal digits precision
double 8 bytes ~15 decimal digits precision
long double 8 or 16 bytes (compiler-dependent) Higher precision floating point
_Bool / bool 1 byte (in <stdbool.h>) Stores 0 (false) or 1 (true)
void * 4 bytes (32-bit) / 8 bytes (64-bit) Generic pointer type
printf("char: %lu bytes\n", sizeof(char));
Incorrect Code Why It’s Wrong Corrected Version
%d expects int, sizeof returns
printf("%d", sizeof(int)); printf("%lu", sizeof(int));
size_t
unsigned long x; printf("%d", x); %d is for int, not unsigned long printf("%lu", x);
Bitwise Operators – AND (&)

o The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers.
o The logic of AND operator is given below:

A B A&B
0 0 0
0 1 0
1 0 0
1 1 1
1 if both are 1

Let, A = 5 (0000 0101)


B = 1 (0000 0001)
A & B = 0000 0001 , which is 1 in decimal

https://iq.opengenus.org/content/images/2021/07/op-table-1.png
Bitwise Operators – AND (&)

Here, input and output are decimal numbers,


but the bitwise operation is done in binary by
default.
Bitwise Operators – AND (&)
In binary:
• Even numbers always end with 0 int num = 7;

• Odd numbers always end with 1 if (num & 1) {


So, if you AND a number with 1: printf("Odd\n");
} else {
• You get 0 if the number is even printf("Even\n");
• You get 1 if the number is odd }

Number Binary num & 1 Result


4 0100 0 Even
7 0111 1 Odd
12 1100 0 Even
21 10101 1 Odd

Faster than num % 2


Bitwise Operators – OR (|)

• The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers.
• The logic of OR operator is given below:

A B A|B
0 0 0
0 1 1
1 0 1
1 1 1
1 if any are 1

Let, A = 5 (0000 0101)


B = 1 (0000 0001)
A | B = 0000 0101 , which is 5 in decimal
Bitwise Operators – OR (|)
Bitwise Operators – OR (|)

● If you do not know the value of a variable, simply do OR operation with 0 and you’ll get to know the value.
● For example, x = 0000 0101 (5)
x = x| 0 = 0000 0101 (5)

int x = 42; // Binary: 00101010


int result = x | 0; // = 00101010

printf("%d\n", result); // Output: 42


Bitwise Operators – NOT (~)

Original (x): 00000000 00000000 00000000 00000101


Bitwise NOT ~x: 11111111 11111111 11111111 11111010

int x = 5; // Binary: 00000000 00000000 00000000 00000101


int result = ~x;
Bitwise Operators – XOR

• The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits are different.
• The logic of XOR operator is given below:

A B A^B
0 0 0
0 1 1
1 0 1
1 1 0
1 if both are different

Let, A = 5 (0000 0101)


B = 1 (0000 0001)
A^B= 0000 0100 , which is 4 in decimal
Bitwise Operators – XOR
Bitwise Operators – Left Shift

• The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second
operand decides the number of places to shift.
• Syntax: x << y
• Bits of x are shifted to the left by y bit. The last y bits are filled with 0.
• Let, int x = 5 (0000 0101)
• x = x << 1; (0000 1010), which is 10 in decimal
Bitwise Operators – Left Shift

Let, x << y
Here, the << (left shift) operator is equivalent to multiplying x by 2y

For example, int x = 3; // Binary: 00000011


5 << 3 = 5 * 23 = 40 int y = x << 2; // Binary: 00001100 → Decimal: 12
2 << 4 = 2 * 24 = 32
// 3 * (2^2) = 3 * 4 = 12

• Only works correctly for non-negative integers


• Can lead to overflow if the result exceeds the data type limit
• Not suitable for floating point numbers

110110 (decimal 54) << 1 = 1101100 (decimal 108) which is 54 * 2 = 108

https://media.geeksforgeeks.org/wp-content/uploads/20240618173455/Left-Shift-in-c-cpp.webp
Bitwise Operators – Right Shift

● The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand
decides the number of places to shift.
● Syntax: x >> y
Bits of x are shifted to the right by y bit. The first y bits are filled with 0.
● Let, int x = 10 (0000 1010)
x = x >> 1; (0000 0101), which is 5 in decimal
Bitwise Operators – Right Shift

● Let, x >> y
Here, the >> (right shift) operator is equivalent to dividing x by 2y
int x = 16; // Binary: 00010000
● For example, int y = x >> 2; // Binary: 00000100 → Decimal: 4
10 >> 1 = 10 / 21 = 5
32 >> 4 = 32 / 24 = 2 // 16 / (2^2) = 16 / 4 = 4

Point Explanation
Works with unsigned and positive signed integers
negative numbers (implementation-dependent
Not always reliable for
behavior)
Rounding It does floor division (discards remainder)
Example

Expression Operation Binary Result Decimal


5&3 AND 0101 & 0011 = 0001 1
5^3 XOR 0101 ^ 0011 = 0110 6
~5 NOT ~00000101 = 11111010 -6 (2's comp)
5 << 1 Left Shift 0101 → 1010 10
5 >> 1 Right Shift 0101 → 0010 2
Bit Mask

https://lh6.googleusercontent.com/proxy/dUeJblCyQ3w3wBem8VrEIJh_XGwR7Z5pJQD-IqVDlBZ4bymfC2L_y36o6lAo9ECzmy_MRB-Q1BrbaxH59Tr8ggdSbBttzCfnbA
https://schoolcoders.com/img/data-representation/numbers/and-mask-bits.png
Checking a Bit using AND

Clear specific bits (set them to 0) without changing other bits


int x = 0b1111; // 15 in binary: 1111
➢ 1 << 2 = 00000100
x = x & ~(1 << 2); // Clear the 3rd bit (bit 2)
➢ ~(1 << 2) = 11111011
➢ x & ~(1 << 2) = 1111 & 11111011 = 1011
printf("%d", x); // Output: 11 (binary 1011)

https://i.ytimg.com/vi/ffPOA7UUDAs/maxresdefault.jpg
Setting a Bit to 1 using OR

https://i.ytimg.com/vi/ffPOA7UUDAs/maxresdefault.jpg
Toggling/Flipping a Bit using XOR

Toggling/Flipping all the Bits using NOT

https://i.ytimg.com/vi/ffPOA7UUDAs/maxresdefault.jpg
Clearing a digit to 0 using OR and NOT

https://i.ytimg.com/vi/ffPOA7UUDAs/maxresdefault.jpg

You might also like