CSE-CSE4202-Lecture1
CSE-CSE4202-Lecture1
Grading
Lab Sessions —Lab Task + Viva,
3 Lab Exams — Lab Quiz-1 + Lab Quiz-2 + Lab Final,
Problem solving — your contest performances and submissions on Codeforces,
and a Final Project!
A binary number is a number expressed in the base-2 numeral system — a positional notation
with a radix of 2.
Only two symbols: 0 and 1.
7 6 5 4 3 2 1 0
MSB LSB
7 6 5 4 3 2 1 0
Data-bits
For signed integers, the MSB is the sign-bit and the rest are all data-bits. (1 for –ve, 0 for +ve)
7 6 5 4 3 2 1 0
These 6 operators work at the bit-level and are used to perform bitwise operations.
& | ^ ~ ≪ ≫
Bitwise AND Bitwise Right Shift
&& || !
It takes two numbers as operands and does AND (&) on every bit of the two numbers.
31 30 7 6 5 4 3 2 1 0
It takes two numbers as operands and does OR (|) on every bit of the two numbers.
31 30 7 6 5 4 3 2 1 0
It takes two numbers as operands and does XOR (^) on every bit of the two numbers.
31 30 7 6 5 4 3 2 1 0
It takes one number as operand and performs 1’s complement NOT (~) on every bit of the number.
31 30 7 6 5 4 3 2 1 0
~ 00 … … 1010 1010 (170)10
It takes two numbers as operands 𝑥 and 𝑁 and shifts (≪) the binary representation of 𝑥 by 𝑁
bit positions to the left.
31 30 7 6 5 4 3 2 1 0
≪ 1
00 … … 1010 10100 (340)10
It takes two numbers as operands 𝑥 and 𝑁 and shifts (≫) the binary representation of 𝑥 by 𝑁
bit positions to the right.
31 30 7 6 5 4 3 2 1 0
≫ 1
000 … … 1010 1010 (85)10
The idea is simple! — Odd numbers have an LSB = 1, whereas even numbers have an LSB = 0.
31 30 7 6 5 4 3 2 1 0
31 30 7 6 5 4 3 2 1 0
The idea is to use (1 ≪ 𝑁) as a mask and then performing Bitwise AND (&).
31 30 7 6 5 4 3 2 1 0
31 30 7 6 5 4 3 2 1 0
| 00 … … 0000 0100 (1 ≪ 2)
31 30 7 6 5 4 3 2 1 0
| 00 … … 0000 0010 (1 ≪ 1)
The idea is to use ~(1 ≪ 𝑁) as a mask and then performing Bitwise AND (&).
31 30 7 6 5 4 3 2 1 0
31 30 7 6 5 4 3 2 1 0
31 30 7 6 5 4 3 2 1 0
| 00 … … 0000 0100 (1 ≪ 2)
The idea is to use (1 ≪ 𝑁) as a mask and then performing Bitwise XOR (^).
31 30 7 6 5 4 3 2 1 0
^ 00 … … 0000 0010 (1 ≪ 1)
31 30 7 6 5 4 3 2 1 0
^ 00 … … 0000 0100 (1 ≪ 2)