tags:

views:

122

answers:

4

Hi all,

main()
{
        char a,b,c;
        a=6;
        b=-5;
        c = a+b ;
        printf ("%d %d %d \n" , a, b,c ) ;
}
Here , what actually happens 

00000110
10000101 
--------
10000111
--------
So values c will -11 , then how come it is 1. 
+5  A: 

Your error is: -5 in two's complement is not 10000101. In 32-bit it is: 11111111111111111111111111111011

6, is:

00000000000000000000000000000110

The sum is:

  11111111111111111111111111111011
+ 00000000000000000000000000000110
  --------------------------------
  00000000000000000000000000000001

I recommend you read this Wikipedia page about signed number representation. Modern CPUs use two's complement.

Eli Bendersky
The point is well made, but a signed char is typically 8 bit, so `(char)-5` is 11111011b.
Clifford
+2  A: 
#include<stdio.h>
    main()
    {
            char a,b,c;
            a=6;
            b=-5;
            c=a+b;
            printf("%d %d %d \n", a,b,c);
    }

    /*
     *
     *  Here 6 value as = 00000110
     *       -5 value will be treated as complement of 5.
     *
     *       Actual 5 value is 00000101
     *       Complement of 5 is 11111010
     *        Then we need to add 1 to the complement of 5
     *        So that time value is 11111011
     *
     *        Now we need to add 5 and 6
     *
     *        00000110
     *        11111011
              ---------
              00000001
    */ 
muruga
+2  A: 

You seem to be a bit confused about how negative values are stored on the computer. What you have there is called "sign and magnitude". Since arithmetic is difficult with sign and magnitude numbers (as you've experienced!), basically all computers use "two's complement." This is easily calculated by following two steps:

  1. Flip all bits (that is, take the complement)
  2. Add one

Doing that, 0000101 becomes 1111011 (in 8 bits, for simplicity). Then addition is easy:

  1111011 = -5
+ 0000110 =  6
 ________ |
  0000001 =  1

Which is 1, as expected.

Daniel G
+6  A: 

It seems that you're confusing the concepts of 2's complement and sign-bit, and bitwise-or and addition in C.

(And who would expect 6 + (-5) ≠ 1…)


(1) 2's complement and sign-bit

On most architectures negative numbers are implemented as a 2's complement of its magnitude. While the C standard does not mandate 2's complement being used, it is the most popular one. In 2's complement, the negative of x is constructed as ~x + 1, e.g.

  5   = 0000 0101
 ~5   = 1111 1010    # ~x means 1's complement, a.k.a. bitwise-NOT
 ~5+1 = 1111 1011    # +1 means add 1 to the integer.

This is the binary representation of -5. The highest bit of the integer is called the sign bit because it determines the sign of the integer. All negative integers in 2's complement has sign bit = 1. But just flipping the sign bit doesn't negate the number. It is merely an indicator that the number is negative or not.


(2) Bitwise-or and addition in C

+ means integer addition in C and all other practical languages, because integer addition much more useful than bitwise-or. So although OR is widely written as + in logical proofs, no programming languages recognize + for bitwise-OR.

In C a char is just a signed integer which is exactly 1-byte long. The behavior is same as all other kinds of signed integers. You have to use | for bitwise-OR.

  0000 0110               0000 0110
+ 1000 0101             | 1000 0101
———————————             ———————————             
  1000 1011               1000 0111
       ^^--- note the carry ---^^
KennyTM
++ for the more thorough answer
Eli Bendersky