0% found this document useful (0 votes)
3 views25 pages

CSE-CSE4202-Lecture1

The document outlines the first lecture of the CSE 4202: Structured Programming II Lab, focusing on binary representations and bitwise operators in C. It covers topics such as binary numbers, signed vs unsigned integers, and various bitwise operations including AND, OR, XOR, NOT, and bit shifting. Additionally, it discusses practical applications of bitwise operators for tasks like parity checking and manipulating specific bits.

Uploaded by

ezyibrahima
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)
3 views25 pages

CSE-CSE4202-Lecture1

The document outlines the first lecture of the CSE 4202: Structured Programming II Lab, focusing on binary representations and bitwise operators in C. It covers topics such as binary numbers, signed vs unsigned integers, and various bitwise operations including AND, OR, XOR, NOT, and bit shifting. Additionally, it discusses practical applications of bitwise operators for tasks like parity checking and manipulating specific bits.

Uploaded by

ezyibrahima
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/ 25

CSE 4202: Structured Programming II Lab

Lecture 1 — Introduction & Bitwise Operators

Syed Rifat Raiyan


Lecturer
Department of Computer Science & Engineering
Islamic University of Technology, Dhaka, Bangladesh
Email: [email protected]

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 1


Introduction
First things first…

Google Classroom Codes


• Group-1A — yn2ymkfu
• Group-1B — yfamciql
• Group-2A — b3aywowz
• Group-2B — xw73is6v

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!

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 2


Lecture Plan
The agenda for today

• Understand binary representations of numbers in C

• Recap how bitwise operators work

• Basic bitmasking and bit manipulation techniques

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 3


Binary Number
What is it?

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.

(42)10 = 0010 1010 2

The indexing of a bitset/bit-field/binary number is done from right-to-left.

7 6 5 4 3 2 1 0

0010 1010 Indices

MSB LSB

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 4


Binary Number
signed vs unsigned integer representations (1-byte)

For unsigned integers, all bits are considered as data-bits.

7 6 5 4 3 2 1 0

1010 1010 27 + 25 + 23 + 21 = (170)10

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

1010 1010 −27 + 25 + 23 + 21 = (−86)10

Sign-bit Data-bits Check its 2’s complement

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 5


Bitwise Operators
What are they?

These 6 operators work at the bit-level and are used to perform bitwise operations.

& | ^ ~ ≪ ≫
Bitwise AND Bitwise Right Shift

Bitwise OR Bitwise XOR Bitwise NOT Bitwise Left Shift

Don’t confuse them with the Logical Operators…

&& || !

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 6


Bitwise AND
How does it work? (for unsigned integers)

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

00 … … 1010 1010 (170)10

& 00 … … 1001 0110 (150)10

00 … … 1000 0010 (130)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 7


Bitwise AND
How does it work? (for signed integers)

This time the MSB will be interpreted as the sign-bit.


31 30 7 6 5 4 3 2 1 0

10 … … 1010 1010 (−2147483478)10

& 10 … … 1001 0110 (−2147483498)10

10 … … 1000 0010 (−2147483518)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 8


Bitwise OR
How does it work? (for unsigned integers)

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

00 … … 1010 1010 (170)10

| 00 … … 1001 0110 (150)10

00 … … 1011 1110 (190)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 9


Bitwise XOR
How does it work? (for unsigned integers)

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

00 … … 1010 1010 (170)10

^ 00 … … 1001 0110 (150)10

00 … … 0011 1100 (60)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 10


Bitwise NOT
How does it work? (for unsigned integers)

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

11 … … 0101 0101 (−171)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 11


Bitwise Left Shift
How does it work? (for unsigned integers)

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

00 … … 1010 1010 (170)10

≪ 1
00 … … 1010 10100 (340)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 12


Bitwise Right Shift
How does it work? (for unsigned integers)

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

00 … … 1010 1010 (170)10

≫ 1
000 … … 1010 1010 (85)10

Code snippet in C Console Output

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 13


Parity Checking
How to use bitwise operators for this?

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

00 … … 1010 1011 (171)10

& 00 … … 0000 0001 (1)10

00 … … 0000 0001 (1)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1010 (170)10

& 00 … … 0000 0001 (1)10

00 … … 0000 0000 (0)10

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 14


Parity Checking
The implementation

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 15


Getting Nth Bit
How to use bitwise operators for this?

The idea is to use (1 ≪ 𝑁) as a mask and then performing Bitwise AND (&).
31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

& 00 … … 0000 0100 (1 ≪ 2)

00 … … 0000 0000 (0)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

& 00 … … 0000 0010 (1 ≪ 1)

00 … … 0000 0010 (2)10

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 16


Getting Nth Bit
The implementation

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 17


Setting Nth Bit
How to use bitwise operators for this?

The idea is to use (1 ≪ 𝑁) as a mask and then performing Bitwise OR (|).


31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

| 00 … … 0000 0100 (1 ≪ 2)

00 … … 1010 1111 (175)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

| 00 … … 0000 0010 (1 ≪ 1)

00 … … 1010 1011 (171)10

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 18


Setting Nth Bit
The implementation

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 19


Resetting Nth Bit
How to use bitwise operators for this?

The idea is to use ~(1 ≪ 𝑁) as a mask and then performing Bitwise AND (&).
31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

& 11 … … 1111 1101 ~(1 ≪ 1)

00 … … 1010 1001 (169)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

& 11 … … 1111 1011 ~(1 ≪ 2)

00 … … 1010 1011 (171)10


CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 20
Resetting Nth Bit
The implementation

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 21


Toggling Nth Bit
How to use bitwise operators for this?

We already have all the tools in our arsenal!


31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

& 11 … … 1111 1101 ~(1 ≪ 1)

00 … … 1010 1001 (169)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

| 00 … … 0000 0100 (1 ≪ 2)

00 … … 1010 1111 (175)10


CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 22
Toggling Nth Bit
The implementation

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 23


Toggling Nth Bit
How to use bitwise operators for this? (a more concise approach!)

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 … … 1010 1011 (171)10

^ 00 … … 0000 0010 (1 ≪ 1)

00 … … 1010 1001 (169)10

31 30 7 6 5 4 3 2 1 0

00 … … 1010 1011 (171)10

^ 00 … … 0000 0100 (1 ≪ 2)

00 … … 1010 1111 (175)10


CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 24
Toggling Nth Bit
The implementation (a more concise approach!)

CSE 4202: Structured Programming II Lab Lecture 1 CSE, IUT 25

You might also like