Team Emertxe
Function and Pointer
Basics
Assignment 4
Assignment 4
Assignment 4
WAP to toggle ‘n’ bits from given position of a number
WAP to toggle ‘n’ bits from given position of a number
Input:
Assignment 4
WAP to toggle ‘n’ bits from given position of a number
Input: Read integer ‘num’, number of bits ‘n’ and position
‘pos’
Assignment 4
WAP to toggle ‘n’ bits from given position of a number
Input: Read integer ‘num’, number of bits ‘n’ and position
‘pos’
Output:
Assignment 4
WAP to toggle ‘n’ bits from given position of a number
Input: Read integer ‘num’, number of bits ‘n’ and position
‘pos’
Output: Return the updated value of ‘num’ after toggling
‘n’ bits from ‘pos’.
Assignment 4
Example’s:
Assignment 4
Example’s:
➢ Input: num = 10 , n = 3 and pos = 5
Assignment 4
Example’s:
➢ Input: num = 10 , n = 3 and pos = 5
➢ Binary format of num 10 :
Assignment 4
Example’s:
➢ Input: num = 10 , n = 3 and pos = 5
➢ Binary format of num 10 :
Assignment 4
0 0 0
1
0
1
0
0
num
Example’s:
➢ Input: num = 10 , n = 3 and pos = 5
➢ Invert 3 bits from position 5 of num 10:
Assignment 4
0 0 0
1
0
1
0
0
7 6 0
1
2
3
4
5 Bit Position
num
Example’s:
➢ Input: num = 10 , n = 3 and pos = 5
➢ Invert 3 bits from position 5 of num 10:
Assignment 4
0 0 0
1
1
1
0
0
0
1
0
50
Updated
‘num’
0 0 0
1
0
num
Example’s:
➢ Input: num = 15 , n = 2 and pos = 2
Assignment 4
Example’s:
➢ Input: num = 15 , n = 2 and pos = 2
➢ Binary format of num 15 :
Assignment 4
Example’s:
➢ Input: num = 15 , n = 2 and pos = 2
➢ Binary format of num 15 :
Assignment 4
0 0 1
1
1
1
0
0
num
Example’s:
➢ Input: num = 15 , n = 2 and pos = 2
➢ Invert 2 bits from position 2 of num 15:
Assignment 4
0 0 1
1
1
1
0
0
7 6 0
1
2
3
4
5 Bit Position
num
Example’s:
➢ Input: num = 15 , n = 2 and pos = 2
➢ Invert 2 bits from position 2 of num 15:
Assignment 4
1
1
0 0 9
1
0 0 1
0
0
1
0 0 1
0
0
num
Updated
‘num’
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
Assignment 4
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Binary format of num 8 :
Assignment 4
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Binary format of num 8 :
Assignment 4
? ? ?
?
?
?
?
?
num
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Binary format of num 8 :
Assignment 4
0 0 0
0
0
1
0
0
num
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Invert 2 bits from position 3 of num 8:
Assignment 4
0 0 0
0
0
1
0
0
7 6 0
1
2
3
4
5 Bit Position
num
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Invert 2 bits from position 2 of num 15:
Assignment 4
0
0
? 0 ?
0
0 0 1
0
0
0
0 0 ?
0
0
num
Updated
‘num’
Example’s:
➢ Input: num = 8 , n = 2 and pos = 3
➢ Invert 2 bits from position 2 of num 15:
Assignment 4
0
0
1 0 4
0
0 0 1
0
0
0
0 0 0
0
0
num
Updated
‘num’
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
Assignment 4
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Binary format of num 9 :
Assignment 4
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Binary format of num 9 :
Assignment 4
? ? ?
?
?
?
?
?
num
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Binary format of num 9 :
Assignment 4
0 0 1
0
0
1
0
0
num
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Invert 1 bit from position 0 of num 9:
Assignment 4
0 0 1
0
0
1
0
0
7 6 0
1
2
3
4
5 Bit Position
num
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Invert 1 bit from position 0 of num 9:
Assignment 4
1
0
0 0 ?
0
0 0 1
0
0
?
0 0 1
0
0
num
Updated
‘num’
Example’s:
➢ Input: num = 9 , n = 1 and pos = 0
➢ Invert 1 bit from position 0 of num 9:
Assignment 4
1
0
0 0 8
0
0 0 1
0
0
0
0 0 1
0
0
num
Updated
‘num’
Sample execution:-
Assignment 4
Sample execution:-
Assignment 4
Pre-requisites:-
Assignment 4
Pre-requisites:-
➢ Bitwise operators
Assignment 4
Pre-requisites:-
➢ Bitwise operators
➢ Functions
Assignment 4
Pre-requisites:-
➢ Bitwise operators
➢ Functions
Objective:-
Assignment 4
Pre-requisites:-
➢ Bitwise operators
➢ Functions
Objective:-
➢ To understand the concept of Bitwise operations.
Assignment 4
Team Emertxe
Thank you

04_toggle_n_bits_from_pos.pdf