pls explain the procedure

# how to find the binary equivalent of a negative decimal number. for instance what is the binary equivalent of -2?

**altaf356**#2

#include<stdio.h>

#include<conio.h>

main()

{

int num,pos;

printf(“Enter a Num

“);

scanf(”%d”,&num);

for(pos=15;pos>=0;pos–)

(num>>pos)&1 ? printf(“1”):printf(“O”);

printf("

");

getch();

return;

}

**kcahdog**#3

binary equivalent of a decimal number is normally represented as 2’s complement of the number. eg. consider negative of -7. the binary equivalent of 5 is 0101 (4 bit representation). to calculate 2’s complement, we first invert all the digits and then add 1 to it. so negative is invert(0101)+1 = 1010 + 1= 1011. the first bit is 1 which shows the number is negative. the restriction is that for 4 bit numbers, only 3 bits are used to represent the magnitude of the number while the first bit shows sign (0 - positive, 1 - negative). i suggest you google it for more info. eg for -2, 2 is represented as 0010. so -2 = 1101+1= 1110.

**altaf356**#4

The Above Code is TurboC, and if it is compiled on gcc,everything remains the same just that the value of pos in for loop should be made 31, instead of 15. As in TurboC, the size of int is 2 Bytes where as in gcc, its 4 Bytes