In: Computer Science
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Code for this program is provided below alongwith the output screenshot. This program takes command line argument and converts the number from decimal to binary 16 bit . If a negative number is given through argument , it represents the number in two'complement .Two's complement representation is widely used in todays systems for representing negative numbers. code is explained through code comments as well as a brief explanation is written below for the program and functions used in it.
#############################################################################
EXPLANATION-->>>
Program takes negative as well as positive numbers and convert them to repective binary representaion . For negative numbers , we convert the binary representation in two's complement and the result is the representation in binary.
For example: consider a number 87, its binary will be 1010111 and in 16 bit we will add 0 in right most bits so the number 87 in binary 16 bit will be 0000000001010111
Now -87 will be represented in two's complement. first we will find binary of 87 that is 0000000001010111
Now we inverse this binary and add 1 , the result will be two's complement or the binary 16 bit of negative 87 :- : 1111111110101000
+1
11111111101001001
11111111101001001 is binary representation of -87
atoi() function
This function is used in program for converting string two number. As the command line argument will be in string format we will convert it to integer by using this function
abs() function
This function is used for taking absolute value of a number. means abs(17)=17 and abs(-17)=17.
All the necessary explanation is in comments in code
##########################################################################
CODE--->>>
#include <stdio.h>
#include <stdlib.h>
//main function taking arguments in arg[] array
int main(int argc,char* arg[])
{
if(argc==1) //if the command line argument is not provided
printf("\n There is no Command Line Argument");
else //if the argument is provided
{
int number1,number2; //int variables
int binary[16]; //binary array to represent individual bits of number
int j=15; //variable to act as iterator for array
number1=atoi(arg[1]); //converting command line argument to integer
//converting to absolute value of number1 (meaning if the number1 is negative change it to positive
number2=abs(number1);
//loop to convert decimal number to binary
while(number2/2 >0 )
{
binary[j--]=number2%2;
number2=number2/2;
}
binary[j--]=number2;
if(j>=0) //putting zero in all the left places of 16 bit number
{
for(int i=j;i>=0;i--)
binary[i]=0;
}
if(number1>=0) //if the number is positive print the binary representation
{
printf("Binary Representation of %d is :\n ",number1);
for(int i=0;i<16;i++)
printf("%d",binary[i]);
}
//if the number is negative than we need to convert the binary into two's complement
else
{
//loop to inverse each bit
for(int i=0;i<16;i++)
{
if(binary[i]==0)
binary[i]=1;
else
binary[i]=0;
}
int carry=1; //variable for carry
//Adding 1 to inverse binary to get two's complement
for(int i=15;i>=0;i--)
{
int sum=0;
sum=binary[i]+carry;
if(sum==2) //for 1+1 because 1+1=0 with carry of 1
{
carry=1;
binary[i]=0;
}
else //for 1+1 or 0+0
{
binary[i]=sum;
carry=0;
}
}
//print this two's complement because negative numbers are represented in twos complement
printf("\nBinary Representation of %d is :\n ",number1);
for(int i=0;i<16;i++)
printf("%d",binary[i]);
}
}
return 0;
}
#######################################################################
OUTPUT-->>