Question
Objectives: Number Systems Bitwise Operations Masking Program: Write a C program to extract information hidden in a whole number. The information pertains to users participation
Objectives:
Number Systems
Bitwise Operations
Masking
Program:
Write a C program to extract information hidden in a whole number. The information pertains to users participation in a contest and winning some prizes. The information hidden in the input number are as follows:
The number of participants are: The lowest 5 bits in the input number.
The number of prizes one can win are: Bits 2, 3, 4 from the right.
The chance of winning in this competition is stored in the 5th bit from right.
1 indicates high chance and 0 indicates low chance.
Function Prototype:
Write function prototypes at the beginning of the program before main().
Write the function definitions (the code) after main().
Required Function: Every function must be preceded with 4 lines of comments describing its name, input(s), output type, and task.
1) get_code ( ) function
Input parameter: None
- Output: int
-Task: prompt the user to enter a whole number in range of -100 and 100, both inclusive. Return the number as output.
Validation: Use a loop to validate the range of the input number.
2) display_binary ( ) function
- Input parameter: int
- Output: void
-Task: Display the input number in 16 bit binary.
Display a space between every 4 bits for clarity.
main( )
Declare an int variable named lottery. Declare other int variables to store the number of participants, number of prizes and etc.
Invoke the function get_code() and store its output in variable lottery.
Display the lottery in hexadecimal (use %X in printf ).
Define a mask to retrieve the number of participants. Tip: 0001 1111
Use this mask and bitwise & to retrieve the number of participants.
Display the number to the screen.
Define a mask to retrieve the number of prizes. Tip: 0000 1110.
Perform a bitwise & between the lottery and the mask, shift the result to the right one bit to get the number of prizes - Display the outcome to the screen.
Define a mask to retrieve the chance of winning in the contest. Tip: 0001 0000
Use the bitwise & between the mask and the lottery, to get the value of the 5th bit. Display the chance of winning to the screen.
Invoke the function display_binary ( ) to display lottery in binary.
Flip the 7th and 8th bits from the right in the number (that is change bit 1 to 0 and bit 0 to 1). Define a mask 1100 0000 and do a bitwise ------- between the mask and the number to flip those bits.
Invoke the function display_binary() to display the updated number to the screen in binary see if those bits are flipped..
Rerun the program. Add rerun loop to allow the user try other codes.
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