×

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

 0 pls explain the procedure asked 05 Jul '13, 21:47 0★imcode 49●20●31●36 accept rate: 0%

 0 #include #include main() { int num,pos; printf("Enter a Num\n"); scanf("%d",&num); for(pos=15;pos>=0;pos--) (num>>pos)&1 ? printf("1"):printf("O"); printf("\n"); getch(); return; }  answered 05 Jul '13, 22:56 0★altaf356 1 accept rate: 0% 1.3k●3●10●25 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 (05 Jul '13, 22:58) altaf3560★
 0 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. answered 05 Jul '13, 23:01 3★kcahdog 10.0k●28●54●129 accept rate: 14%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×164
×17
×4
×4

question asked: 05 Jul '13, 21:47

question was seen: 6,595 times

last updated: 05 Jul '13, 23:13