Question
USING C++ write a short program to do the following: 1. Ask for an integer 2. Read the integer from the keyboard 3. Display the
USING C++
write a short program to do the following:
1. Ask for an integer
2. Read the integer from the keyboard
3. Display the binary representation of the integer
4. Optional: Repeat the above until the user chooses to quit
Use the bitwise logical operations to perform this:
& - this is bitwise and... each bit is ANDed with the corresponding bit to produce the result
| - bitwise or performs the OR operation on each pair of bits in the first and second arguments
<< - shift left, not a dance step, it move the bits in the left value by the amount of the right one
>> - shift right, same as the above, only shift them to the right
Make a function that takes an integer as a parameter:
void printBinary(int theValue) {
Inside the function, we will have a loop to check each of the 32 bits in the integer value. In pseudocode it is:
for (i = 31; i >= 0; i--) { // note we are looping from high bits to low if ((thevalue >> i & 1) > 0) { // if the bit at position i is one print "1 " // print a one followed by a space } else { // otherwise the bit is zero print "0 " // print a zero followed by a space } } // end of the for loop, but we still need to print a newline print a newline character
NOTE: We are not actually printing the bits, we are checking to see if they are zero or one and then printing the appropriate character to the screen. Also, if you are using C++ and Visual Studio, make sure your project is an x64 project, not an x86 project, otherwise there may be some problems with data sizes.
Make sure to declare a local variable for "i" and your function should work fine. This is similar to the method needed to print out the sets for the bit string program. However, in the bit string program we will print out "i" rather than a zero or one, and we will loop from 0 to 9 rather than 31 to 0.
Here is some sample output from a possible programming solution:
Welcome to Waldo's Bitopia program Please enter an integer to display the bit representation: 255 The bit representation of 255 is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 Enter Zero to quit, any other integer to do it again: 1 Please enter an integer to display the bit representation: 1234567 The bit representation of 1234567 is: 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1 1 Enter Zero to quit, any other integer to do it again: 2 Please enter an integer to display the bit representation: -255 The bit representation of -255 is: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 Enter Zero to quit, any other integer to do it again: 0
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