Question: C++ ONLY PROGRAM MUST BE C++ Challenge Problem 2 (optional): This will test whether you really understand binary numbers, computer arithmetic, and looping. Write a
C++ ONLY PROGRAM MUST BE C++
Challenge Problem 2 (optional): This will test whether you really understand binary numbers, computer arithmetic, and looping. Write a program which prompts the user for a positive integer, reads it in, prints it out, and then prints it's representation in binary (i.e., O's and one's). There are several ways to get the state of any bit in a number. One way is to use bit-wise and'ing (look it up! & instead of &&). You can also do it with a little creative use of division and the modulus operator. Hint: what does the following do? int n = 13; bool b; /binary 0000 1101, 1*8 1 4+0 2 +11 bs ( (n % 8) / 4); // what does % 8 do? then divide it by 4 Add some looping, and you should be able to figure out how to output a number in binary. You can do it easily and get the bits in reverse order, using cout 1. Either will right-shift by 1 bit // and repeat until n/2 is zero. But that will give you the digits in reverse order. Your challenge is to figure out a way to get them out in forward order! Call your program "binary_values.cpp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
