Question

Write a C program, that would take a negative number as a input from the console,...

Write a C program, that would take a negative number as a input from the console, and convert it into 2’s complement binary representation. Recall that, there are two steps to that:

● 1’s complement of the positive bitwise representation, which is similar to bit flipping.

○ Find out the appropriate bitwise operator for that, or do it manually.

● Add 1 with the 1’s complement number.

○ Simply add 1 with the number you get in the previous step. And for this problem, assume that the number is 5 bits long. Obviously, the most significant bit is used for the sign. Sample input output are:

Input Output
-12 10100
-9 10111
-7 11001
-16 10000
0 0
Add a comment Improve this question Transcribed image text
Answer #1
C Program
#include<stdio.h>
// function declaration
void getBinary(int, int[]);
void flipBits(int[]);
void addBit1(int[]);

// main function
int main()
{
    int i;
    int number;
    int binary[] = {0,0,0,0,0};
    printf("Enter the number: ");
    scanf("%d",&number);
    getBinary(number, binary);
    flipBits(binary);
    addBit1(binary);
    printf("2's complement is: ");
    for(i = 0; i < 5; i++)
       printf("%d", binary[i]);
    printf("
");
}

// function to convert a decimal to binary and
// store it into an array of integers 0,1
void getBinary(int number, int binary[])
{
    int index = 4;
    if(number < 0)
      number = number * -1;
    while(number > 0)
    {
        int r = number % 2;
        binary[index--] = r;
        number /= 2;
    }
}

// function to flip bits of the binary array
void flipBits(int binary[])
{
    int i;
    for(i = 0; i < 5; i++)
    {
        if(binary[i] == 0)
           binary[i] = 1;
        else
           binary[i] = 0;
    }
}

// function to add binary 1 to the
// array
void addBit1(int binary[])
{
    int i;
    int rem = 0;
    // loop from back to front of the array
    for(i = 4; i >= 0; i--)
    {
        // extract the bit at i'th position
        int bit = binary[i];
        // if at iteration i = 4 (at first iteration)
        if(i == 4)
        {
            // if bit is 1
            if(bit == 1)
            {
                // make that bit to 0
                binary[i] = 0;
                // store the remainder
                rem = 1;
            }
            else // else simply make the bit to 1
                binary[i] = 1;  
        }
        // for all other iterations
        else
        {
            if(bit == 1 && rem == 1)
                binary[i] = 0;

            else if(bit == 0 && rem == 1)
            {
                binary[i] = 1;
                rem = 0;
            }   
        }
    }
} 

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a C program, that would take a negative number as a input from the console,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program (java): that reads a 8-bit String input, must be read in as such....

    Create a program (java): that reads a 8-bit String input, must be read in as such. Then convert that string of 1s and 0s to decimal as if it were encoded using the following representations: Unsigned 1's Complement 2's Complement Q(4.4) (2's complement) 1 bit (sign) - 3 bits (exponent: use bias) - 4 bits (significand), implied binary point w/ 1. Also check for 0.... Do not use helper functions, must be done iterating string and modifying an int/long value....

  • please include only the digits of the appropriate number system. In particular, do not precede the...

    please include only the digits of the appropriate number system. In particular, do not precede the answers with ‘0x’ or ‘0b’ or follow your answers with base indicators, like subscript 2 or 10. 1. A processor uses 24 bits for its memory addressing. How many possible distinct locations (in decimal) can the computer address? The computer memory address locations are numbered from 0 to the maximum. If a memory locations' address is (7243)10, how is this address represented in binary...

  • (a) Write a truth table. The input is 4-bit binary ABCD, A is MSB, D is...

    (a) Write a truth table. The input is 4-bit binary ABCD, A is MSB, D is LSB. The output is also represented by x. (b) Obtain an output expression in the form of a SOP. (c) Use Boolean Algebra to design a circuit consisting of only four inverters, four 3-input and gate, and one 4-input OR gate using the simplified and simplified expression obtained in (b). 4-6. The Excess-3 coding system is a four-bit digital coding system for encoding all...

  • MIPS Programming 1 In this project, you are going to write a MIPS program to read...

    MIPS Programming 1 In this project, you are going to write a MIPS program to read an integer number and convert it to 8- bit signed binary in a string. Your program should do the followings: Part II (50%, due date: March 1) for negative number . Write a non-leaf function to convert a negative integer number to 2's complement in string . Two parameters: integer number, string array or pointer -Get absolute value of the negative number o Call...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...

    C++ Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering system we use is called the decimal system, or base 10 system. The numbering system that the computer uses is called the binary system, or base 2 system. The purpose of this exercise is to write a function to convert a string representing a binary number from base 2 to base 10. To convert a...

  • Please show that it works and show the output. Description Develop a program (in C++) that...

    Please show that it works and show the output. Description Develop a program (in C++) that first asks the user to input a signed number, for example 48, then converts it to binary number by using Two's Complement regulations. Do the same for the second inputted number, eg. 17. Then compute the summation of these two numbers, output the results in binary format first, then if there is an overflow, display error message; otherwise, convert the result to decimal value...

  • 5.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    5.14 LAB: Convert to binary Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the...

  • Q2 (20pts) Design a combinational ct that accepts an input 3-bit binary number (XYZ) and generate...

    Q2 (20pts) Design a combinational ct that accepts an input 3-bit binary number (XYZ) and generates an output 4-bit binary number (ABCD) where output equal to the double of the input number. (a) Construct the truth table (b) State each output-bit as a function in sum of minterms (SOM) form: (c) State each output-bit as a function in product of maxterms (POM) form: ΠΜ(.) (d) Optimize the circuit using K-maps and find the simplified functions Show your work full-credit. Q3...

  • Bit Counting Machine A bit counting machine reads a binary input vector (a, b, c, d,...

    Bit Counting Machine A bit counting machine reads a binary input vector (a, b, c, d, e) and produces a binary number (s2, s1, s0) that counts the number of ”1”s in the input bits. For example when (a, b, c, d, e) = (0, 1, 1, 1, 1), we have output (s2, s1, s0) = (1, 0, 0), and when (a, b, c, d, e) = (1, 1, 0, 1, 0), we have output (s2, s1, s0) = (0,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT