Skip to main content

Thread: Does any one tried this


hey friends
1 have tried this. open python in terminal .
types follows
>>>~1
ans:-2
>>>~-1
ans:0

how going done above .

it's way two's complement signed numbers work. ~ bitwise negate.

simple example 4 bits:

code:
  1 = 0001  ~1 = 1110 = -2   -1 = 1111 ~-1 = 0000 = 0
the bit in red denotes sign: 1 means negative, 0 means positive.

point of two's complement can same low-level addition algorithm positive , negative numbers, i.e. simple long addition in binary:

code:
(2 + 3 = 5):  0010 +  0011  ---- 0101    (0 + 1 = 1, 1 + 1 = 0 , 1 carried)   (2 + (-3) = -1):  0010 +  1101  ---- 1111    (0 + 1 = 1, 1 + 0 = 1, 0 + 1 = 1 , 0 + 1 = 1)


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Does any one tried this


Ubuntu

Comments