NO

Author Topic: 1's Complement  (Read 3189 times)

Ryuzaki33

  • Guest
1's Complement
« on: December 22, 2012, 09:49:28 AM »
Hi,
The following code prints -13.Am not sure y.
12 is 1100 in binary.Its one's complement is 0011. Should n't the answer be 3?Including the 5th bit,the complement would be 10011 which can be interpreted as -13(-16+3) or -19(-(16+2+1)).Whats the general rule used here and which bit is considered as the sign bit for a given number.

#include<stdio.h>
int main(void)
{
int b;
int a=12;
b=~a;

printf("%d\n",b);
}

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: 1's Complement
« Reply #1 on: December 22, 2012, 12:02:47 PM »
You are using an integer that is 32bits.
your 12 is ecoded as: 00000000000000000000000000001100
1's complement is:      11111111111111111111111111110011 = -13
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: 1's Complement
« Reply #2 on: December 22, 2012, 03:19:03 PM »
Whats the general rule used here and which bit is considered as the sign bit for a given number.

Frankie has given you the exact answer... the sign bit is the most significant bit of the type ... bit 7 in char, bit 15 in short ints, bit 31 in ints, bit 63 in long long ints.

Remember computers don't work in decimal, as we do... for them it's all about binary.

(Hi Frankie!)