Question: in c++ Write a program that reads in a decimal number n. Convert the decimal number into binary format and print out the number in
in c++
Write a program that reads in a decimal number "n". Convert the decimal number into binary format and print out the number in binary format. Then, write the following functions to get, set and clear bit at the position "index" and display the corresponding output given below.
/* Retrieve a bit from a number "n" in binary format at position "index" Input: number n, position index where 0 being the right most(least significant) bit Output: bit at position "index" Example: Input: n=10 (in binary 1010), index=0, output: 0*/
int getBit(int n, int index)
/* Set a bit at position "index". This will set a bit to 1 if its 0 or else it will remain unchanged Input: number "n", position "index" where 0 being the right most(least significant) bit Output: Output the binary number after a bit is set at position "index" Example: Input: n=10 (in binary 1010), index=0, output: 11 (in binary 1011)*/
int setBit(int n, int index)
/* Clear a bit at position "index". This will set a bit to 0 if it is 1 or else it will remain unchanged. Input: number "n", position "index" where 0 being the right most(least significant) bit Output: Output the binary number after a bit is cleared at position "index" Example: Input: n=10 (in binary 1010), index=2, output: 10 (in binary 1010)*/
int clearBit(int n, int index)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
