Question: Let's attempt implementing an algorithm we have seen before: decimal to binary conversion ( 8 bits only ) Create a function that takes in an

Let's attempt implementing an algorithm we have seen before: decimal to binary conversion (8 bits only)
Create a function that takes in an unsigned int as its input and has a void output.
The function will print the binary value of the unsigned int.
Reference slides 39 and 40 from this week.
ALGORITHM: Pick either this algorithm or the alternative one (you only have to implement one)
Do not use a recursive solution or your own procedure.
Start with an exponent of 128.
while(exponent >=1)
if (n exponent)
print 1
n=n-exponent
else if (n exponent)
print 0
exponent = exponent ?2;
Alternative Algorithm - This one may be harder, but more interesting
There is an alternative method you may consider (but may be more difficult).
You may extract the actual bit from the unsigned int as shown on slide 47 from this week.
Beware Endianness (ordering of bits).
This means you may find the bitsThis means you may find the bits in the wrong order and need to reverse it.
For example: 4 may print as 00100000
Reverse your loop to have it print 00000100
Create a main function that reads in an unsigned int value from the user.
Pass the value to the function so its binary code is printed.
NOTE: Creating your own function is required for full credit. The input should be an unsigned int and the output should be void. The function simply prints 1's and 0's to the screen depending on the input given.
Example Screenshots
Enter a number: 4
Binary output: 00000100
Enter a number: 37
Binary output: 00100101
Enter a number: 127
Binary output: 01111111
 Let's attempt implementing an algorithm we have seen before: decimal to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!