home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] Double checking re: twos complement & signed types ??

  • Subject: Re: [OCLUG-Tech] Double checking re: twos complement & signed types ??
  • From: William Case <billlinux [ at ] rogers [ dot ] com>
  • Date: Fri, 21 Sep 2007 13:38:29 -0400
Hi Brenda, Bart et al;

> umm, well all the numbers are stored as two's complement.
> 
> > I.E. For me, all I would probably ever use is an unsigned integer (or
> > short or float or double) and the subtract operator -- correct??
> 
I am dropping the above issue.  The use of signed integers will probably
become self-evident once I have solved the rest.
> 
> Usually, when you feel like you're trying to nail down flies and missing
> all the time, you have the wrong mental model.
> 
Brenda, that is the essence of my problem, I know.

> 
> (Warning:  use fixed-width font to make any sense out of the following:)
> 
>                          | the same representation|
>  -8 -7 -6 -5 -4 -3 -2 -1 | 0  1  2  3  4  5  6  7 <-- (signed)
>             (unsigned) ->  0  1  2  3  4  5  6  7 | 8  9 10 11 12 13 14 15
> 
>   1  1  1  1  1  1  1  1 | 0  0  0  0  0  0  0  0 | 1  1  1  1  1  1  1  1
>   0  0  0  0  1  1  1  1 | 0  0  0  0  1  1  1  1 | 0  0  0  0  1  1  1  1
>   0  0  1  1  0  0  1  1 | 0  0  1  1  0  0  1  1 | 0  0  1  1  0  0  1  1
>   0  1  0  1  0  1  0  1 | 0  1  0  1  0  1  0  1 | 0  1  0  1  0  1  0  1
> 
> 
> (note, binary representation of each number is in a vertical column below
> the number.)
> 
> There are only 2^4 values you can put in 4 bits - the difference in
> signed and unsigned is in how you interpret those values.
> 
> The values of 0-7 for both signed and unsigned are the same.  This is the
> part of the signed and unsigned number range that overlaps.
> 
> 
> Note that the column for -1 is identical to the one for 15
> Note that the column for -2 is identical to the one for 14
> Note that the column for -3 is identical to the one for 13
> etc.
> 
> When you add 0001 to 0111, you get 1000
>         and the status register shows (N:1  Z:0  C:0  V:1 )
> 
> Depending on what the compiler decides, that can be interpreted as 8 or -8
> 
> When you add 0001 to 1111, you get 0000
>         and the status register shows (N:0  Z:1  C:1  V:0 )
> 
> Zero is always 0.  But, you might have added 1 to 15, or added 1 to -1.
> 
> "Two's complement" is another name for the ascii-art picture I drew, and
> the fancy formula they give you is just a way to calculate what the
> picture shows.
> 
> 
> The status bits show:
> 
> N:  when the high bit is set in the result
> Z:  when the number is all zeroes in the result
> C:  when the arithmetic "carries" as an unsigned
>     (goes over the 0 -> -1 boundary or over the 15 -> 0 boundary)
> V:  when the arithmetic "overflows" as a signed
>     (goes over the -8 -> -9 boundary or over the 7 -> 8 boundary)
> 
> Hope this helps.
> 
It helps a lot.  They layout of differing values above is the clearest,
most logical I have seen presented in either my texts or online.  It is
the visual chart that I will keep in my memory from now on.  The status
bit is usually dealt with quite summarily and is, I think, the key to
understanding.  However, I really don't believe grasping the basics of
two's complement is my problem.  I was comfortable with the concept
months ago and binary arithmetic is simple once you have climbed into
it.  But there is something I am missing between typing in a constant
numerical value in source code, or typing in a dynamic value at the
command line, and the consequent outcome of a mathematical operation.

My only recourse seems to be get another text book (the ones I have deal
at great length with the theory of two's complement, but are confusing
and short on how it is implemented) and read through it and see if I can
come up with a head slapping eureka moment.

If that doesn't work, I'll have to corner somebody at the next beer sig,
buy them a beer, and get out the paper napkins and a pen.
 
By the way, these are not fruitless chases.  I keep a short list of my
computer "unfathomables".  It remains short because eventually they all
get solved. 

Brenda, this post does not need a reply.  I am just keeping you up to
date on where things stand and because I said I would respond to your
post in the morning.

-- 
Regards Bill