Question
C PROGRAM need same output 1- WAP to print 'n' bits from LSB of a number Description: a. Read number num from user. b. Read
C PROGRAM need same output
1- WAP to print 'n' bits from LSB of a number
Description:
a. Read number num from user. b. Read number n from user. c. Do error checking -> If n is greater than integer size, assign n value as sizeof integer. d. Print n number of bits of num from LSB end. If num is 10 and n is 12, then print last 12 bits of binary representation of 10. The output should be -> 0 0 0 0 0 0 0 0 1 0 1 0
Pre-requisites:
Bitwise operators
Functions
Sample Execution:
Test Case 1:
Enter the number: 10
Enter number of bits: 12
Binary form of 10: 0 0 0 0 0 0 0 0 1 0 1 0
Test Case 2:
Enter the number: 15
Enter number of bits: 4
Binary form of 15: 1 1 1 1
requested files:
#include
int print_bits(int, int);
int main() { int num, n; printf("Enter num, n : "); scanf("%d%d", &num, &n); printf("Binary form of %d:", num); print_bits(num, n); }
//
2 - WAP to put the (b-a+1) lsbs of num into val[b:a]
Description:
Read number num from user.
Read number val from user.
Read number a from user.
Read number b from user(a <= b <=31)
Do error checking
Check b is within limit or not.
Call replace_nbits_from_pos function by passing val, b - a + 1, b and n as arguments.
replace_nbits_from_pos(num, a, b, val);
Print the new value of i.
Prompt for continue option.
Example: If num = 11 = (0 0 0 0 1 0 1 1) 2 val = 174 = (1 0 1 0 1 1 1 0) 2 a = 3, b = 5 No_of_bits = b a + 1 = 5 3 + 1 = 3, get 3 bits from 11(num) and replace 3(No_of_bits)from 5th(b) postion of 174(val) then New 'val' value = (1 0 0 1 1 1 1 0) 2 = 158
Pr-requisites:-
Bitwise Operators
Functions
Objective: -
To understand the concept of
Functions
Bitwise Operators
Inputs: - Integers num, val, a, b
Sample execution: - Test Case 1: Enter the value of 'num' : 11 Enter the value of 'a' : 3 Enter the value of 'b' : 5 Enter the value of 'val': 174 Result : 158
requested file
#include
int replace_nbits_from_pos(int, int, int, int);
int main() { int num, a, b, val, res = 0; printf("Enter num, a, b, and val:"); scanf("%d%d%d%d", &num, &a, &b, &val); res = replace_nbits_from_pos(num, a, b, val); printf("Result = %d ", res); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started