You are not logged in. Please login at www.codechef.com to post your questions!

×

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

pls explain the procedure

asked 05 Jul '13, 21:47

imcode's gravatar image

0★imcode
49203136
accept rate: 0%


#include<stdio.h>
#include<conio.h>
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;
}
link

answered 05 Jul '13, 22:56

altaf356's gravatar image

0★altaf356
1
accept rate: 0%

edited 05 Jul '13, 23:13

sunny_patel's gravatar image

2★sunny_patel
1.3k31025

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★

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.

link

answered 05 Jul '13, 23:01

kcahdog's gravatar image

3★kcahdog
10.0k2854129
accept rate: 14%

edited 05 Jul '13, 23:02

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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