5. WAP to replace ‘n’ bits of a given number
Input:
Assignment 2
6. WAP to replace ‘n’ bits of a given number
Input: Read integer ‘num’, number of bits ‘n’ and ‘val’
Assignment 2
7. WAP to replace ‘n’ bits of a given number
Input: Read integer ‘num’, number of bits ‘n’ and ‘val’
Output:
Assignment 2
8. WAP to replace ‘n’ bits of a given number
Input: Read integer ‘num’, number of bits ‘n’ and ‘val’
Output: Return the updated value of ‘num’ after replacing
‘n’ bits
Assignment 2
11. Example’s:
➢ Input: num = 10 , n = 3 and val = 12
Step 1: Clear 3 bits from LSB of num 10 :
Assignment 2
12. Example’s:
➢ Input: num = 10 , n = 3 and val = 12
Step 1: Clear 3 bits from LSB of num 10 :
Assignment 2
0 0 0
1
0
1
0
0
0 0 0
0
0
1
0
0
num
num
13. Example’s:
➢ Input: num = 10 , n = 3 and val = 12
Step 2: Take 3 bits from LSB end of ‘val’ 12 :
Assignment 2
0 0 0
0
1
1
0
0
val
14. Example’s:
➢ Input: num = 10 , n = 3 and val = 12
Step 3: Replace those 3 bits in the LSB end of num 10 :
Assignment 2
0 0
0
0
1
1
0
0 0 0
1
12
Updated
‘num’
16. Example’s:
➢ Input: num = 15 , n = 2 and val = 1
Step 1: Clear 2 bits from LSB of num 15 :
Assignment 2
17. Example’s:
➢ Input: num = 15 , n = 2 and val = 1
Step 1: Clear 2 bits from LSB of num 15 :
Assignment 2
0 0 1
1
1
1
0
0
0 0 0
0
1
1
0
0
num
num
18. Example’s:
➢ Input: num = 15 , n = 2 and val = 1
Step 2: Take 2 bits from LSB end of val 1 :
Assignment 2
0 0 1
0
0
0
0
0
val
19. Example’s:
➢ Input: num = 15 , n = 2 and val = 1
Step 3: Replace those 2 bits in the LSB end of num 15 :
Assignment 2
1
0
0 1 13
1
0 0 1
0
0
Updated
‘num’
21. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 1: Clear 1 bit from LSB of num 7 :
Assignment 2
22. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 1: Clear 1 bit from LSB of num 7 :
Assignment 2
0 0 1
1
1
0
0
0
0 0 ?
1
1
0
0
0
num
num
23. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 1: Clear 1 bit from LSB of num 7 :
Assignment 2
0 0 1
1
1
0
0
0
0 0 0
1
1
0
0
0
num
num
24. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 2: Take 1 bit from LSB end of val 2 :
Assignment 2
0 0 0
1
0
0
0
0
val
25. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 3: Replace that 1 bit in the LSB end of num 7 :
Assignment 2
0
1 0 ?
1
0 0 0
0
0
Updated
‘num’
26. Example’s:
➢ Input: num = 7 , n = 1 and val = 2
Step 3: Replace that 1 bit in the LSB end of num 7 :
Assignment 2
0
1 0 6
1
0 0 0
0
0
Updated
‘num’
28. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 1: Clear 2 bits from LSB of num 8 :
Assignment 2
29. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 1: Clear 2 bits from LSB of num 8 :
Assignment 2
0 0 0
0
0
1
0
0
0 0 ?
?
0
1
0
0
num
num
30. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 1: Clear 2 bits from LSB of num 8 :
Assignment 2
0 0 0
0
1
0
0
0 0 0
0
1
0
0
num
num 0
0
31. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 2: Take 2 bits from LSB end of val 7 :
Assignment 2
0 0 1
1
1
0
0
0
val
32. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 3: Replace those 2 bits in the LSB end of num 8 :
Assignment 2
1
? ? ?
0
0 0 1
0
0
Updated
‘num’
1
33. Example’s:
➢ Input: num = 8 , n = 2 and val = 7
Step 3: Replace those 2 bits in the LSB end of num 8 :
Assignment 2
1
1 1 11
0
0 0 1
0
0
Updated
‘num’
1