Question: Create a recursive function in C++ called convertToBinary that will take a decimal number (can assume short int) and return a string of binary digits.

Create a recursive function in C++ called "convertToBinary" that will take a decimal number (can assume short int) and return a string of binary digits. Since it is short int, the string will be of size 16 assume the number is positive. Your function will pad the most significant bits with zeros for smaller integers. Have the main function input the numbers 16, 128 and 3215 (can be from standard input or hardcoded in the "convertToBinary" function call) -- you can assume the input is not negative for now. Your function has to pad for 16 bits.

Your program will take for input the numbers 16, 128 and 3215. It will output:

0000000000010000

0000000010000000

0000110010001111

Have the "convertToBinary" function handle a negative number, converting it to a two's complement binary (padded for 16 bits). Please assume the input is always between the range of 0 to 32,767 for the initial question; and between -32,768 and a maximum value of 32,767 for the bonus.

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!