1

I have read some previous questions in the past, but still, I could not work out the step I have to do

Bit not operation in PHP(or any other language probably)

What is “2's Complement”?

$a = -9;
echo ~$a; //print 8

If I understand correctly,the 1st step is to convert to binary. In binary, it is 1001. The NOT operator then convert from 1001 to 0110.

What do I have to do after this? Do I add a 1? I am lost in all the explanation given in the explanation.

Or is it simpler to make an educated guess (in the exam condition). If I take -9, (step 1) convert to 9 and (step 2) then take away 1. That would give me 8. Is this a correct pattern to work out the answer?

user9718914
  • 103
  • 9
  • Thank you so much, Viney, for putting the effort to illustrate the answer with the diagrams. Now, I understand that we have to convert back to 0 <-> 1 the second time to get the correct answer. This step will help me in the exam to check for the correct answer. Thank you so much. – user9718914 May 29 '18 at 09:16

1 Answers1

1

Well it's very easy but first you must note the fact that if you system is 32 bit you cannot use all 32 bits to represent the number rather just 31, same for 16 bit and 8 bit systems ; the rightmost bit is never used to denote the magnitude of the number but rather just the sign.

There are some areas where you can tell the system to use all the bits for magnitude so 2's complement isn't used as such and all numbers will be assumed positive but normally for the most "regular" business we use signed numbers

Here's an example for an 8-bit systems

Step 1. Covert it to binary (you did that already)

enter image description here

You can see since it's binary going right to left each place value is double of the next in contrast to decimal where each place value is 10 times of its predecessor

Step 2. Invert all the bits form the result from previous step

enter image description here

Step 3. Now add 1 to the accumulated result from the previous step

enter image description here

Now ~ to above result will give 00001000 ie 8 Why so?

Because you are using BITWISE operator it knows nothing of the underlying number it just does what it should - inverting rest of the things like how to make sense of this 1s0s is upto the system which does a reverse 2s complement but since it's a postive number (MSB is 0) so it knows there's no need to do anything it just converts it to decimal but had it been negative it would have done all the computation to reverse the 2s complement because 2s complement of positive number is same as that number.

Now note that if you are using signed number as in above you can use 8 bits to represent number from -128 to 127 so to represent 256 you must upgrade to 32 bit.

A fun thing is to note that in 2's complement form for positive numbers more 1s means greater magnitude whereas for negative numbers 0s mean more magnitude

It would be hard at first to interpret 2s complement directly so start first converting every 2's complement to binary then interpret it as decimal once you are proficient you can directly tell what a 2s complement form amounts to in decimal

Ref.

http://sandbox.mc.edu/~bennet/cs110/tc/tctod.html https://www.rapidtables.com/convert/number/binary-to-decimal.html http://onlinecalculators.brainmeasures.com/Numbers/TwosComplimentCalculations.aspx https://www.quora.com/Why-computer-use-2s-complement-to-store-negative-number-instead-of-1s-complement

Update: To answer your comment : above isn't applicable to positive numbers.2s complement of positive number is that same number itself you might ask why so? well it's because 2s complement was invented to mitigate the problem of subtraction so computers really don't have to implement a SUB circuit separately (in fact there isn't any SUB logic gate at all! all we have is AND OR and NOT that's why it's hard to implement a SUB circuit)

So now same circuit could be used for addition as well as subtraction which uses 2s complement 'hack' in to perform subtraction by doing addition!

So 1 is stored as 00000001 now when you NOT it ie ~00000001 gives11111110 now that -2 in 2s complement form

Vinay
  • 7,442
  • 6
  • 25
  • 48
  • I tried the step above and I could not get the right result for this example.. Have I missed any step? $a = 1; echo ~$a; //should print -2 One in binary = 00000001 Step 1: convert 0 to 1 and vice versa 11111110 Step 2~: Add 1 11111111 Step 3: convert again 00000000 – user9718914 May 29 '18 at 09:45
  • Because that's not how it's stored in memory see the update – Vinay May 29 '18 at 17:22