2

I wonder how I implement the 2-complements method for binary-aritmetics when the number that is subtracted is less than the other number. For instance - If I want to subtract 13 from 12 ... i.e 12 -13 = -1.

I have tried the following steps:

1) 12 to binary using 8 bits (0000 1100)

2) -13 to binary using 2-complements:

a) 13 into binary using 8 bits: 0000 1101

b) invert: 1111 0010

c) add 1: 1111 0010 + 1 = 1111 0011

So if I calculated correctly the result (1111 0011) represents -13. So lets add this negative representation with 12:

  00001100 + 11110011 = 11111111

So I do not understand the result - only only ones:s (1111...)

This happens when the result is negative. Obviously I missed something.

Grateful for help!!!!

OGHaza
  • 4,795
  • 7
  • 23
  • 29
  • Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more details you provide, the more answers you are likely to receive. – kkuilla Jan 24 '14 at 15:01
  • 1
    @kkuilla he's not even asking for code just an explanation – OGHaza Jan 24 '14 at 15:14
  • 1
    OP, it seems to me that `11111111` is the correct answer `-1` – OGHaza Jan 24 '14 at 15:19
  • @OGHaza yes, but only if he interprets the value as signed. Otherwise, it's 255. – OnoSendai Jan 24 '14 at 15:20
  • 1
    @OnoSendai But he's already interpreting the -13 as signed early in the calculation, but yeah you're right, that must be where the misunderstanding is. – OGHaza Jan 24 '14 at 15:21
  • @OGHaza Yes, you're correct - but he forgot to interpret the result the same way; that's what I was pointing out. =) – OnoSendai Jan 24 '14 at 15:21
  • Off-topic: @kkuilla seems to be doing the rounds with that comment ([comment activity](http://stackoverflow.com/users/2545927/kkuilla?tab=activity&sort=comments))...... – OGHaza Jan 24 '14 at 15:25
  • Hey, it's on the clipboard, would be a waste not to paste... ;) – OnoSendai Jan 24 '14 at 15:26
  • Oh,yes. I do. Quite a bit, Where applicable. I think it is a good introduction when somebody asks for code rather than just saying "Show the code". Fair enough. I can accept that some think that I was wrong. I have no problems with that. – kkuilla Jan 24 '14 at 15:27

1 Answers1

2

Congratulations, you've just discovered the difference between signed/unsigned integer representations!

Let's take, for example, the Uint16 format (unsigned 16 bit). It can represent any integer value from 0 (binary 0000000000000000) to 65535 (binary (1111111111111111).

So, what happens when you add one to 65535? You'll need another Uint16 to express that value, and the first will be all zeroes again:

0000000000000001 0000000000000000

Now, the signed version of Uint16, Int16, may range from −32,768 to 32,767. Why? Because it may use its most significant bit to determine the signal.

Like this, for Int8 (signed integer, 8 bits), which happens to be exactly the format you're using on your example:

00000000 -> 0
00000001 -> 1
00000010 -> 2
[...]
01111110 -> 126
01111111 -> 127
10000000 -> -128 (MSB set - number is negative)
10000001 -> -127
10000010 -> -126
[...]
11111110 -> -2
11111111 -> -1 <- (This is your result!)

Notice that you're already taking that into consideration when you do step B - by inverting you're setting the MSB to 1, thus indicating that the value is negative.

What's happening to your calculations is that you'll reach a state of arithmetic overflow if you don't take into consideration the signal bit.

Here's some links that may help with your issue:

Bitwise Operator on positive and negative numbers

Signed number representations - Wikipedia

Integer (computer science) - Wikipedia

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • Thanks a lot for your help!!! But I still dont get it how the computer (processor) knows **when** to interpret the value as signed? I know it because I can see it with my eyes - but how does the computer know WHEN to treat 11111111 as a negative number instead of treating it as 255? – user3232476 Jan 25 '14 at 11:05
  • You may benefit form this link then, @user3232476: http://en.wikipedia.org/wiki/Overflow_flag – OnoSendai Jan 26 '14 at 22:23
  • And this one also: http://stackoverflow.com/questions/4645230/would-doing-arithmetic-operation-on-a-pair-of-signed-and-unsigned-numbers-be-leg – OnoSendai Jan 27 '14 at 14:16