Question: This is my C programming code: #include int main() { long num, remainder, base = 1, binary = 0, no_of_1s = 0; scanf(%ld, &num); if(num>=0){

This is my C programming code:

#include

int main()

{

long num, remainder, base = 1, binary = 0, no_of_1s = 0;

scanf("%ld", &num);

if(num>=0){

while (num > 0)

{

remainder = num % 2;

/* To count no.of 1s */

if (remainder == 1)

{

no_of_1s++;

}

binary = binary + remainder * base;

num = num / 2;

base = base * 10;

}

//printf("%ld ", decimal_num);

printf("0b0%ld ", binary);

}else{

num = -num;

while (num > 0)

{

remainder = num % 2;

binary = binary + remainder * base;

num = num / 2;

base = base * 10;

}

char c[33];

int i=0;

for(i=0;i

c[i] = '1';

}

c[32] = '\0';

int count = 31;

int addRemain;

int first = 1;

while(binary>0){

remainder = binary%10;

if(remainder==1){

remainder = 0;

}else{

remainder =1;

}

if(addRemain==1){

if(remainder==1){

remainder = 0;

addRemain = 1;

}else{

remainder = 1;

addRemain = 0;

}

}

if(first==1){

if(remainder==1){

remainder = 0;

addRemain = 1;

}else{

remainder = 1;

addRemain = 0;

}

}

c[count] = '0'+remainder;

binary = binary/10;

count--;

first = 0;

}

printf("0b%s",c);

}

return 0;

}

for the following question:

This is my C programming code: #include int main() { long num,

remainder, base = 1, binary = 0, no_of_1s = 0; scanf("%ld", &num);

Some of the answers show up as correct but others do not.

Here are the ones that need to be corrected:

1) In my code, the answer for 0 (3 bits) is "0b00" but it needs to be "0b000"

2) In my code, the answer for -3 (3 bits) is "0b11111111111111111111111111111101" but it needs to be "0b101"

3) In my code, the answer for 256 (32 bits) is "0b0100000000" but it needs to be "0b00000000000000000000000100000000"

Please help!

Write a program that takes an integer from stdin, and then prints the binary representation (with a leading eb to denote the format is binary). Negative numbers should be represented in two's complement. The total bit length is given as the first argument to your program, and leading zeros should not be truncated You may assume that input is valid (i.e. you do not need to validate input), and the number given is always within the range of the bit length required to represent the number. You may also assume that the maximum bit length given ill be 32 Examples Text ./dec2bin 5 12 0b01100o Text ./dec2bin 4 5 0b0101 Text ./dec2bin 3 ob011 Text ./dec2bin 3 0b000 Text ./dec2bin 3 0b101 Text ./dec2bin 32 256

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!