Question

In: Computer Science

A program called i2b.c was implemented with the intention to provide a binary representation of an...

A program called i2b.c was implemented with the intention to provide a binary representation of an integer. Unfortunately, the implementation has the following limitation:

  • It was hard-coded to only convert a single number.
  • The conversion algorithm only works for positive number.

Modify the existing source code of i2b.c such that:

  • i2b.c can now accept a single command line argument. This command line argument is assumed to be a valid signed integer.
  • i2b.c will convert the command line argument into its corresponding binary representation with the following condition:
    • The representation is an array of integers.
    • This array has a length of 32.
    • This array contains the binary representations of the integer argument.
    • Index 0 contains the most significant bit, and index 31 contains the least significant bit.
  • i2b.c will print out the final binary representation on a single line without any space between the bits.

PROGRAM BELOW

#include <stdio.h>

#define N 32

int main(int argc, char *argv[]) {

  int n = 12345;

  int binRep[N];

  int i;

  for (i = 0; i < N; i++) {

    binRep[i] = 0;

  }

  i = 0;

  while (n > 0) {

    binRep[i] = n % 2;

    n = n / 2;

    i++;

  }

  for (i = N - 1; i >= 0; i--) {

    printf("%d", binRep[i]);

  }

  printf("\n");

  return 0;

}

Solutions

Expert Solution

// according to your code

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{ 
    if (argc != 2){
        puts("usage: i2b <number>");
        return -1;
    }

    int32_t n = atoi(argv[1]);

    int binaryNum[32] = {}; 
    int i = 0; 
    
    while (n > 0 && i < 32) { 
        binaryNum[i] = n % 2; 
        n = n / 2; 
        i++;
    } 
    
    if(n >= 0){
        binaryNum[31]=0;
    } else {
        binaryNum[31]=1;
    }

    for (int i = 31; i > -1; i--){
        printf("%d", binaryNum[i]);
    }
    
    puts("");
    return 0; 
}
/*better implementation
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(int argc, char *argv[]){ 

    if (argc != 2){
        puts("usage: i2b <number>");
        return -1;
    }

    int32_t number = atoi(argv[1]);

    for(int i = 31; i > -1; i--){
        printf("%d", (number >> i) & 1);
    }
    puts("");
    return 0;
}

*/


Related Solutions

Provide the instruction type, assembly language instruction, and binary representation of the instruction described by the...
Provide the instruction type, assembly language instruction, and binary representation of the instruction described by the following LEGv8 fields: op = 0x458, Rm = 14, Rn = 16, Rd = 18, shamt = 0
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Look for the binary representation for each of the numbers in the left column of the...
Look for the binary representation for each of the numbers in the left column of the following table using the algorithm that iteratively divides by two. Show all steps of the algorithm for each number in different rows of a table with three columns like the example I have included. All steps are essential for getting full credit. Create 5 different tables for the 5 numbers. The numbers in the left column of the following table are expressed in base-10...
i.Show that 0.0100112 is the binary representation of 0.29687510 ii.A computer system allows only 4 binary...
i.Show that 0.0100112 is the binary representation of 0.29687510 ii.A computer system allows only 4 binary digits after the decimal point. Write the two binary values with exactly 4 digits after the decimal point that ‘sandwich’ 0.0100112. iii.Using the two binary ‘sandwich’ values that you found in (ii), write down the minimum and maximum binary values that sandwich the result of 0.0100112 + 0.0100112 iv.Convert each of the binary values you found in (iii) to decimal notation. Give the minimum...
(6 pts) Convert the data representation given below • Convert 10110111 unsigned binary representation, to decimal...
(6 pts) Convert the data representation given below • Convert 10110111 unsigned binary representation, to decimal representation. • Convert 01100000101110000001010111111000 the binary representation to a hexadecimal representation. • Convert 0xBAAADA55 hexadecimal representation, to a binary representation. 2. (8 pts) Complete the following arithmetic operations in two’s complement representation. What are the values of the carry flag and the overflow flag? (Assume a six-bit system) • 31 + 11 • 13 – 15 • (-2) x (-16) • (-15) ÷ 5
Prove that every real number with a terminating binary representation (finite number
Prove that every real number with a terminating binary representation (finite number of digits to the right of the binary point) also has a terminating decimal representation (finite number of digits to the right of the decimal point).  
What is the 11 bit, binary representation of -108? What is the hexadecimal equivalent of this...
What is the 11 bit, binary representation of -108? What is the hexadecimal equivalent of this number?
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3...
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3 ) * ( 3 - 2 ) ) / ( ( 3 + 10 ) + ( ( 8 - 3 ) - 2 ) ) * 9 )" -Give an O(n)-time algorithm for computing the depth of all positions of a tree T, where n is the number of nodes of T.
A particular cipher is implemented by combining the ASCII representation of plaintext characters with pseudorandom bytes...
A particular cipher is implemented by combining the ASCII representation of plaintext characters with pseudorandom bytes (eight-bit binary strings of 1s and 0s) using the XOR function. In the process of encrypting a message, a character in the plaintext, a capital X, is XORed with the pseudorandom byte 10110100. a. What is the ciphertext (in binary form) generated by the encryption of the character ‘M’? (Please show your work.) b. How is the plaintext for this encrypted J recovered? (Please...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT