5. WAP to put the (b-a+1) lsb’s of num into val[b:a]
Input:
Assignment 6
6. WAP to put the (b-a+1) lsb’s of num into val[b:a]
Input: Read integers ‘num’, ‘val’, ‘a’ and ‘b’.
Assignment 6
7. WAP to put the (b-a+1) lsb’s of num into val[b:a]
Input: Read integers ‘num’, ‘val’, ‘a’ and ‘b’.
Output:
Assignment 6
8. WAP to put the (b-a+1) lsb’s of num into val[b:a]
Input: Read integers ‘num’, ‘val’, ‘a’ and ‘b’.
Output: Return the updated value of ‘val’ after replacing
number of bits from position ‘b’.
Assignment 6
12. Example’s:
➢Input: num = 11, val = 174, a = 3 and b = 5
➢Binary format of num 11 :
Assignment 6
0 0 1
1
0
1
0
0
num
13. Example’s:
➢Input: num = 11 ,val = 174, a = 3 and b = 5
Step 1: Find ‘n’ number of bits to be replaced in ‘val’:
n = b-a+1 = 5-3+1 = 3
Assignment 6
14. Example’s:
➢Input: num = 11 ,val = 174, a = 3 and b = 5
Step 2: Clear 3 bits from ‘pos’ 5 of ‘val’ 174 :
Assignment 6
1 0 0
1
1
1
0
1
0
0
0
1 0 0
1
1
val
val
15. Example’s:
➢Input: num = 11 ,val = 174, a = 3 and b = 5
Step 3: Fetch 3 bits from LSB end of ‘num’ 11 :
Assignment 6
0 0 1
1
0
1
0
0
num
16. Example’s:
➢Input: num = 11 ,val = 174, a = 3 and b = 5
Step 4: Replace those 3 bits from 5th position of ‘val’ 174:
Assignment 6
1 0
1
1
1
1
1
0 1 0
0
158
Updated
‘val’
26. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
➢Binary format of num 16 :
Assignment 6
? ? ?
?
?
?
?
?
num
27. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
➢Binary format of num 16 :
Assignment 6
0 0 0
0
0
0
1
0
num
28. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 1: Find ‘n’ number of bits to be replaced in ‘val’:
n = b-a+1 = 3-2+1 = 2
Assignment 6
29. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 2: Clear 2 bits from ‘pos’ 3 of ‘val’ 15 :
Assignment 6
0 0 1
1
1
0
0
0 1
0
?
0
0 0 1
1
?
0
val
val
30. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 2: Clear 2 bits from ‘pos’ 3 of ‘val’ 15 :
Assignment 6
0 0 1
1
1
0
0
0 1
0
0
0
0 0 1
1
0
0
val
val
31. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 3: Fetch 2 bits from LSB end of ‘num’ 16 :
Assignment 6
0 0 0
0
0
0
1
0
num
32. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 4: Replace those 2 bits from 3rd position of ‘val’ 15:
Assignment 6
0 0
0
0
?
?
0
0 1 1 ?
Updated
‘val’
33. Example’s:
➢Input: num = 16, val = 15, a = 2 and b = 3
Step 4: Replace those 2 bits from 3rd position of ‘val’ 15:
Assignment 6
0 0
0
0
0
0
0
0 1 1 3
Updated
‘val’
36. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
➢Binary format of num 13 :
Assignment 6
? ? ?
?
?
?
?
?
num
37. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
➢Binary format of num 13 :
Assignment 6
0 0 1
0
1
1
0
0
num
38. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 1: Find ‘n’ number of bits to be replaced in ‘val’:
n = b-a+1 = 5-3+1 = 3
Assignment 6
39. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 2: Clear 3 bits from ‘pos’ 5 of ‘val’ 25 :
Assignment 6
0 0 1
0
0
0
0
0 1
1
?
?
0 0 1
0
0
?
val
val
40. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 2: Clear 3 bits from ‘pos’ 5 of ‘val’ 25 :
Assignment 6
0 0 1
0
0
0
0
0 1
1
0
0
0 0 1
0
0
0
val
val
41. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 3: Fetch 3 bits from LSB end of ‘num’ 13 :
Assignment 6
0 0 1
0
1
1
0
0
num
42. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 4: Replace those 3 bits from 5th position of ‘val’ 25:
Assignment 6
0 0
0 1
0
?
?
? 0 1 ?
Updated
‘val’
1
43. Example’s:
➢Input: num = 13, val = 25, a = 3 and b = 5
Step 4: Replace those 3 bits from 5th position of ‘val’ 25:
Assignment 6
0 0
0 1
0
1
0
1 0 1 41
Updated
‘val’
1