473,499 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with negative numbers


I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.

Let's assume we're working with an 8-Bit signed integer, and that it
contains no padding.

Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).

The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
unsigned integer would represent the number.

But if the number is negative, then there's separate methods to correlate
the negative bit-pattern with its corresponding positive value:

(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.
It seems we get the following ranges for each format: (Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
(3) Two's complement: -128 through +127
It would seem that each number system has the following advantages:

(1) Sign-magnitude:

Efficient conversion from negative to positive.

(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.

(3) Two's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Are there any other advantage/disadvantages to be aware of?

The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000
How does the machine handle comparison of these two variables? Would it
interpret:

if ( var1 == var2 )

as:

if ( !(var1 & 127 || var2 & 127) )

I'd ask more question as people reply...
--

Frederick Gotham
Jun 27 '06 #1
39 3960
Frederick Gotham wrote:
It seems we get the following ranges for each format:
(Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
(3) Two's complement: -128 through +127
Yes, except that implementations
are allowed to have SCHAR_MIN equal to -127,
regardless of representation.

The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude,
and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000

How does the machine handle comparison of these two variables?


A machine has two choices,
an integer object with the representation for negative zero,
either compares equal to zero
or has a trap representation.

--
pete
Jun 27 '06 #2
pete posted:

var1 == 0000 0000
var2 == 1000 0000

How does the machine handle comparison of these two variables?


A machine has two choices,
an integer object with the representation for negative zero,
either compares equal to zero
or has a trap representation.

Nicely put.

Does that mean that you MUST have padding within a signed integer in order
for "negative zero" to be a trap representation? The reason I ask is as
follows: I recall reading something recently along the lines of "The value
representation bits of an integer type shall not be involved in trapping".
This makes me think that, if you're going to have a trap representation,
then you must have at least one padding bit.
--

Frederick Gotham
Jun 27 '06 #3
Frederick Gotham schrieb:
I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.

Let's assume we're working with an 8-Bit signed integer, and that it
contains no padding.

Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).

The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
domestic?
unsigned integer would represent the number.

But if the number is negative, then there's separate methods to correlate
the negative bit-pattern with its corresponding positive value:

(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.
Note that the last rule does not cover
10000000
whereas the rule "pretend that unsigned representations of values
in the range 1<<7 to (1<<8)-1 are treated as 'unsigned value -
(1<<8)'" does.

Note that C90 does not prescribe one of these three, so you could
come up with "sign magnitude - 1" or "sign plus inverted order
value bits" or ...
C99 restricts your choice to 1s complement, 2s complement, and
sign-magnitude.
It seems we get the following ranges for each format: (Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
Wrong. Reread your rule above. Ones complement covers
-127 to 127
(3) Two's complement: -128 through +127
It would seem that each number system has the following advantages:

(1) Sign-magnitude:

Efficient conversion from negative to positive.
Easy multiplication/division of negative and positive numbers.
Symmetric range.

(2) One's complement:

There's only one bit-pattern for zero.
Wrong. 11111111 fits your definition.
One extra unit in the negative range.
Wrong.
Advantages: ~ means the same as unary minus.
Symmetric range.
(3) Two's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Often "compute through overflow" possible to extend range.

Disadvantages:
- >> for arithmetic shift means "round down
division" instead of real division.
- asymmetric range: You pay simply too much for the "one extra
unit". Having to write -32767-1 in order to make sure that you
arrive at the right value with the right type simply is a
little bit too much. This can incur many ugly checks and can be
for many things as annoying as "value preserving instead of
unsigned preserving".[*]
Are there any other advantage/disadvantages to be aware of?
Sure -- depending on your application. See above for a few of
them.
The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000
Yes. The implementation can treat 10000000 (or, for 1sC, 11111111)
as "trap representation" or "negative zero".
The C99 standard specifically deals with "negative zero" (I am too
lazy to look it up, have a look at N1124.pdf; I am also too lazy
to find out what you can find in C90).
How does the machine handle comparison of these two variables? Would it
interpret:

if ( var1 == var2 )

as:

if ( !(var1 & 127 || var2 & 127) )


This is the implementation's problem, not yours, so you are going
toward off-topic.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 27 '06 #4


Frederick Gotham wrote On 06/27/06 17:42,:
I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.

Let's assume we're working with an 8-Bit signed integer, and that it
contains no padding.

Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).
You're on shaky ground when you designate the sign bit
as the "most significant." It'd be clearer just to say that
the eight-bit number has one sign bit and seven value bits.
The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
unsigned integer would represent the number.
Again, "next" is an iffy proposition best left alone.
(And there's that peculiar use of "domestic" again; it's
not a usage I've ever encountered except in your writing.)
But if the number is negative, then there's separate methods to correlate
the negative bit-pattern with its corresponding positive value:

(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.
Instead of thinking about shuffling the bits, think about
the seven value bits encoding a non-negative value V and the sign
bit specifying a transformation of that value. If the sign bit
is zero there is no transformation and the value of the eight-bit
integer is V. If the sign bit is one

(1) Signed magnitude: The value is -V
(2) Ones' complement: The value is V-127
(3) Two's complement: The value is V-128
It seems we get the following ranges for each format: (Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
(3) Two's complement: -128 through +127
(1) and (3) are right, but the range for (2) is -127
through 127. The bit patterns all-zero and all-one both
represent zero (all-one is informally called "minus zero").

In theory, the ranges supported by C can be "smaller"
than these. The "minus zero" patterns for (1) and (2) and
the -128 pattern for (3) could be trap representations rather
than actual attainable values. I'm not personally familiar
with any C implementations that do such a thing, but that
doesn't prove there aren't any.
It would seem that each number system has the following advantages:

(1) Sign-magnitude:

Efficient conversion from negative to positive.
This won't be a consideration for small integers.
The circuitry to negate a value that can fit in a machine's
accumulator will do the job quite quickly; getting the value
to and from the accumulator will probably dominate.
(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.
There are two bit patterns for zero, and no extra unit.
Ones' complement has a (possibly insignificant) disadvantage
in that some operations require an "end-around carry" where
a carry out of the most significant bit must be added back
in at the least significant position. Since that position
can also be an input to determining the carry, a little more
delay may be required -- but if the whole thing completes in
less than a clock cycle, it doesn't matter how much or how
little "spare time" remains.
(3) Two's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
That last seems to be the big attraction, big enough to
outweigh the inconvenience of the asymmetrical range.
Are there any other advantage/disadvantages to be aware of?

The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000
Yes, these would both represent the value zero.
How does the machine handle comparison of these two variables? Would it
interpret:

if ( var1 == var2 )

as:

if ( !(var1 & 127 || var2 & 127) )


How the machine handles the comparison is a matter for
the circuit designers and the compiler writers. What C
requires is that any two zero values generated by "proper"
means must compare equal to each other, less than all positive
integers, and greater than all negative integers.

This may just fall out of the way the machine's comparator
works. If it doesn't, C will need to take steps to "normalize"
the values before comparing them, transforming "minus zero"
to "canonical zero." There's a possible surprise here, in
that a C implementation might avoid the problem by operating
the arithmetic unit in a mode that never produces "minus
zero" as a result: that way, all zeroes are already canonical,
and the compiler need not generate normalization code. But
if you use type-punning shenanigans to generate a "minus
zero" representation by devious means, that zero might or
might not behave the way a pedigreed C zero must. (At least,
I don't see any guarantee; others might be able to find one.)

--
Er*********@sun.com

Jun 27 '06 #5
Frederick Gotham wrote:

pete posted:
var1 == 0000 0000
var2 == 1000 0000

How does the machine handle comparison of these two variables?
A machine has two choices,
an integer object with the representation for negative zero,
either compares equal to zero
or has a trap representation.


Nicely put.

Does that mean that you MUST have padding within
a signed integer in order
for "negative zero" to be a trap representation?


No.
A signed magnitude representation
with the sign bit is set,
can either be equal to zero or not represent any number.
All bits set in one's complement, is the same way.
The reason I ask is as follows:
I recall reading something recently along the lines of "The value
representation bits of an integer type shall
not be involved in trapping".


How about:
no arithmetic operation on valid
values can generate a trap representation other than as
part of an exception such as an overflow
?

--
pete
Jun 27 '06 #6
pete wrote:
A signed magnitude representation
with the sign bit is set,
with only the sign bit set,
can either be equal to zero or not represent any number.
All bits set in one's complement, is the same way.


--
pete
Jun 27 '06 #7
Frederick Gotham wrote:
"negative zero"


ISO/IEC 9899

6.2.6.2 Integer types

2

If the sign bit is one, the value shall be
modified in one of the following ways:
— the corresponding value with sign bit 0 is negated
(sign and magnitude);
— the sign bit has the value -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (one’s complement).
Which of these applies is implementation-defined,
as is whether the value with sign bit 1 and all value bits zero
(for the first two), or with sign bit and all value bits 1
(for one’s complement),
is a trap representation or a normal value.
In the case of sign and magnitude and one’s complement,
if this representation is a normal value it is called a
negative zero.

3 If the implementation supports negative zeros,
they shall be generated only by:
— the &, |, ^, ~, <<, and >> operators with arguments
that produce such a value;
— the +, -, *, /, and % operators where one argument
is a negative zero and the result is zero;
— compound assignment operators based on the above cases.

It is unspecified whether these cases actually generate a
negative zero or a normal zero, and whether a negative zero
becomes a normal zero when stored in an object.

4 If the implementation does not support negative zeros,
the behavior of the &, |, ^, ~, <<, and >> operators with
arguments that would produce such a value is undefined.

--
pete
Jun 28 '06 #8
Michael Mair posted:
domestic?

Eric Sosman posted:
(And there's that peculiar use of "domestic" again; it's
not a usage I've ever encountered except in your writing.)

Must be a dialectal thing (I'm Irish, but a native speaker of English).

If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?

Hope that helps! : )
Another thing: up until a week or two ago, I used to always say:

-127 to 127 inclusive

But now (in my posts at least), I say:

-127 through 127

I like the ring to it, plus it's very precise. If I wanted to be equally
precise using "to", I'd have to say something like:

-127 inclusive to 127 inclusive
"through" doesn't get such usage in Ireland.
[More off-topic stuff...]

I was in the US last year, Masachusets (sorry about the spelling!),
Colarado and New York; I heard a lot of "I work Monday through Friday"
over there. In Ireland, we say "I work Monday to Friday", even though
it's ambiguous as to whether me mean inclusive, exclusive, or a
combination of both.

But by FAR the hardest things to come to grips with when travelling is
the use of slang, and also vocabulary which is mutually unknown to each
other -- things like "sweater" instead of "jumper", "trainers" instead of
"runners", "band-aids" instead of "plasters".
--

Frederick Gotham
Jun 28 '06 #9
Frederick Gotham said:

<snip>

If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?


Aha! All is now clear.

The canonical term for that is "canonical".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 28 '06 #10
Frederick Gotham <fg*******@SPAM.com> writes:
Michael Mair posted:
domestic?

Eric Sosman posted:
(And there's that peculiar use of "domestic" again; it's
not a usage I've ever encountered except in your writing.)

Must be a dialectal thing (I'm Irish, but a native speaker of English).

If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?

Hope that helps! : )


Interesting. In my version of English (and I suspect in most other
people's as well), "domestic" means roughly "from this country", as
opposed to "imported". I've never heard your usage before you
mentioned it here a few days ago.

[...]
But by FAR the hardest things to come to grips with when travelling is
the use of slang, and also vocabulary which is mutually unknown to each
other -- things like "sweater" instead of "jumper", "trainers" instead of
"runners", "band-aids" instead of "plasters".


The British slang terms for what we call "erasers" and "cigarettes"
can also cause some frivolity.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 28 '06 #11

Frederick Gotham wrote:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.


This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?

signed magnitude is taking the top half
of the integers, flipping them over and moving them to the
bottom, with the zeros overlapping. (From this description,
it's far more obvious why 2's complement is simpler.)

One's complement is derived by moving the top half
to the bottom and shifting up by one to make the zeros
overlap.

It feels like those descriptions are not clear to the clueless
newbie, however. I'm not sure they're even clear to anyone
unless they already understand the number system...

Jun 28 '06 #12
Here, my 2 cents. It is not short but I think, from my experience
with students, it allows to understand the rationale behind the definition:

signed magnitude is taking the top half of the integers, and it is
reassigning them a new meaning as negative values, with the constraint
that each (but one) of these negative numbers has a corresponding
positive partner, uniquely identified by the fact that, when they are
added, with the usual rules for sums, the result gets all bits equal to
zero.

This way, one has an operative definition and, starting from -1, a few
examples allow to fully understand how it works. Of course, one has to
stress that the choice of considering 1000...00 as the minimum negative
number (the only negative number without positive partner) is somewhat
arbitrary, although convenient to make the whole system uniform (
sequences starting with 1 should be considered as representing negative
numbers ), thus simplifying the implementation of comparison between
numbers.

Then, one can easily derive the simple algorithm (toggle each bit,
then add 1) as an effective way of finding the corresponding
sign-partner of any integer.

Giorgio
Jun 28 '06 #13
Giorgio Pastore <pa*****@univ.trieste.it> writes:
Here, my 2 cents. It is not short but I think, from my experience
with students, it allows to understand the rationale behind the
definition:

signed magnitude is taking the top half of the integers, and it is
reassigning them a new meaning as negative values, with the constraint
that each (but one) of these negative numbers has a corresponding
positive partner, uniquely identified by the fact that, when they are
added, with the usual rules for sums, the result gets all bits equal
to zero.


Your describing two's-complement, not signed magnitude.

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 28 '06 #14
Richard Heathfield posted:
Frederick Gotham said:

<snip>

If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?


Aha! All is now clear.

The canonical term for that is "canonical".

Hmm... I'll have to stick that one in my vocabulary!

So what's the canonical way of eating a banana? : )
--

Frederick Gotham
Jun 28 '06 #15
Eric Sosman posted:

(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.


There are two bit patterns for zero, and no extra unit.

So what use is One's complement at all? If it still has two values for
zero, and has no extra unit in the negative range, then what possible good
can it do? It seems that Two's complement would be superior to it in every
way.

--

Frederick Gotham
Jun 28 '06 #16
Bill Pursell wrote:

Frederick Gotham wrote:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.


This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?

signed magnitude is taking the top half
of the integers, flipping them over and moving them to the
bottom, with the zeros overlapping. (From this description,
it's far more obvious why 2's complement is simpler.)

One's complement is derived by moving the top half
to the bottom and shifting up by one to make the zeros
overlap.

It feels like those descriptions are not clear to the clueless
newbie, however. I'm not sure they're even clear to anyone
unless they already understand the number system...


I already understand the number system.
I don't understand the following terms:
"top half of the integers"
"moving them to the bottom"

--
pete
Jun 28 '06 #17
Keith Thompson wrote:

Frederick Gotham <fg*******@SPAM.com> writes:
Michael Mair posted:
domestic?

Eric Sosman posted:
(And there's that peculiar use of "domestic" again; it's
not a usage I've ever encountered except in your writing.)

Must be a dialectal thing (I'm Irish, but a native speaker of English).

If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?

Hope that helps! : )


Interesting. In my version of English (and I suspect in most other
people's as well), "domestic" means roughly "from this country", as
opposed to "imported". I've never heard your usage before you
mentioned it here a few days ago.

[...]


I had teachers that talked like that.

"Domestic" is the opposite of the of "exotic".

The primary and secondary meanings of exotic are
"foreign" and "unusual".

"Domestic" being used as the opposite
of the secondary meaning of "exotic", is exotic.

--
pete
Jun 28 '06 #18
Frederick Gotham wrote:

Eric Sosman posted:
(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.
There are two bit patterns for zero, and no extra unit.


So what use is One's complement at all?


My guess is that the ~ (bitwise complement operator)
was a comparitively fast operation on early cpu's.
If it still has two values for
zero, and has no extra unit in the negative range,
then what possible good can it do?
It seems that Two's complement would be superior to it in every
way.


I think you sentiment is reflected in the number
of systems using two's complement versus one's complement.

--
pete
Jun 28 '06 #19
Frederick Gotham wrote:
Richard Heathfield posted:
Frederick Gotham said:

<snip>
If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?

Aha! All is now clear.

The canonical term for that is "canonical".


Hmm... I'll have to stick that one in my vocabulary!

So what's the canonical way of eating a banana? : )


You fire it out of a cannon in to your mouth ;-)

For what it's worth, I've not come across your usage of the word
domestic either.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jun 28 '06 #20
pete posted:

Interesting. In my version of English (and I suspect in most other
people's as well), "domestic" means roughly "from this country", as
opposed to "imported". I've never heard your usage before you
mentioned it here a few days ago.

[...]


I had teachers that talked like that.

"Domestic" is the opposite of the of "exotic".

The primary and secondary meanings of exotic are
"foreign" and "unusual".

"Domestic" being used as the opposite
of the secondary meaning of "exotic", is exotic.

Yeah, that kind of gets a handle on it.

There's an exotic way of eating a banana (e.g. like Kevin Spacey in K-Pax
who didn't remove its skin first), and there's the domestic way of eating
a banana (i.e. removing the skin and eating the stuff inside).
--

Frederick Gotham
Jun 28 '06 #21
Frederick Gotham wrote:
Eric Sosman posted:
(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.


There are two bit patterns for zero, and no extra unit.


So what use is One's complement at all? If it still has two values for
zero, and has no extra unit in the negative range, then what possible good
can it do? It seems that Two's complement would be superior to it in every
way.


Two's complement has one glaring disadvantage not shared
by the other allowed representations: it has an asymmetrical
range, with more negative values than positives.

Can you spot the error in this silly little number-printer?

#include <stdio.h>
void outnum(int num) {
if (num < 0) {
putchar ('-');
num = -num;
}
if (num > 10)
outnum(num / 10);
putchar ('0' + num % 10);
}

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 28 '06 #22
Bill Pursell wrote:
Frederick Gotham wrote:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.

This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?


Think of the sign bit in two's complement as representing
twice the value of the highest-order value bit, but with a
negative sign. In sixteen-bit two's complement, the bits
are "worth"

bit 0: 1
bit 1: 2
bit 2: 4
...
bit 13: 8192
bit 14: 16384
sign bit: -32768

The value of the integer is the sum of the values corresponding
to all its one-bits.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 28 '06 #23
In article <44***********@mindspring.com> pf*****@mindspring.com writes:
Frederick Gotham wrote:

....
So what use is One's complement at all?


My guess is that the ~ (bitwise complement operator)
was a comparitively fast operation on early cpu's.


And it made multipliers for signed integers much easier. Almost all early
computers were either 1's complement or sign-magnitude.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 28 '06 #24
In article <fb*******************@news.indigo.ie> Frederick Gotham <fg*******@SPAM.com> writes:
....
There's an exotic way of eating a banana (e.g. like Kevin Spacey in K-Pax
who didn't remove its skin first), and there's the domestic way of eating
a banana (i.e. removing the skin and eating the stuff inside).


Merriam-Webster:
domestic, adjective:
1. Living near or about human habitations (tame, domesticated)
2. Of, relating to, or originating with a country and especially
one's own country
3. of or relating to the household or the family
4. devoted to home duties and pleasures
5. indigenous

indigenous, adjective
1. having originated in and being produced, growing, living or
occurring naturally in a particular region or environment
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 28 '06 #25
Eric Sosman posted:

Can you spot the error in this silly little number-printer?

#include <stdio.h>
void outnum(int num) {
if (num < 0) {
putchar ('-');
num = -num;
^^^^

I presume this is the error?

If the range were -128 through 127, and it the value of num were -128,
then we can't represent its positive counterpart.

What does the Standard have to say about doing that? Implementation-
defined behaviour, or undefined behaviour?

}
if (num > 10)
outnum(num / 10);
putchar ('0' + num % 10);
}

--

Frederick Gotham
Jun 28 '06 #26
In article <Yc******************************@comcast.com> Eric Sosman <es*****@acm-dot-org.invalid> writes:
Bill Pursell wrote:

....
This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?


Think of the sign bit in two's complement as representing
twice the value of the highest-order value bit, but with a
negative sign. In sixteen-bit two's complement, the bits
are "worth"

bit 0: 1
bit 1: 2
bit 2: 4
...
bit 13: 8192
bit 14: 16384
sign bit: -32768


And for 1's complement make it -32767 (i.e. one more than the value for
2's complement).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 28 '06 #27
Keith Thompson <ks***@mib.org> wrote:
...
Interesting. In my version of English (and I suspect in most other
people's as well), "domestic" means roughly "from this country", as
opposed to "imported". I've never heard your usage before you
mentioned it here a few days ago.


Domestic bliss vs. imported bliss? ;)

(English my native is not language either.)

Jun 28 '06 #28
In article <J1********@cwi.nl>, Dik T. Winter <Di********@cwi.nl> wrote:
...
There's an exotic way of eating a banana (e.g. like Kevin Spacey in K-Pax
who didn't remove its skin first), and there's the domestic way of eating
a banana (i.e. removing the skin and eating the stuff inside).
Merriam-Webster:
domestic, adjective:


Obviously the OP's use of "domestic" is an extension of the original
use. In Britain the phrase "common or garden" (originally, I assume,
applied to plant varieties) is used in this way, so that I wouldn't be
very surprised to hear someone refer to, say, a "common or garden sort
algorithm". But I doubt that "domestic" will pass into common usage
for this.

-- Richard
Jun 28 '06 #29
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Interesting. In my version of English (and I suspect in most other
people's as well), "domestic" means roughly "from this country", as
opposed to "imported".
As far as I can tell, that's predominantly a U.S. usage. It seems to
be creeping into use in Britain through airports ("domestic flights",
formerly "internal").
The British slang terms for what we call "erasers" and "cigarettes"
can also cause some frivolity.


We just made them up to confuse Americans.

Incidentally, the material "rubber" is so called because rubbing-out
(=erasing) was the first use of it, rather than the rubbers (=erasers)
being so-called because they were made of rubber.

-- Richard
Jun 28 '06 #30
Frederick Gotham <fg*******@SPAM.com> wrote:
pete posted:
I had teachers that talked like that.

"Domestic" is the opposite of the of "exotic".

The primary and secondary meanings of exotic are
"foreign" and "unusual".

"Domestic" being used as the opposite
of the secondary meaning of "exotic", is exotic.


Yeah, that kind of gets a handle on it.

There's an exotic way of eating a banana (e.g. like Kevin Spacey in K-Pax
who didn't remove its skin first), and there's the domestic way of eating
a banana (i.e. removing the skin and eating the stuff inside).


Oh. That's not the canonical way, that's the vanilla way. In this case
they happen to be the same, but that's not always the case.

Richard
Jun 28 '06 #31
Dik T. Winter posted:

Merriam-Webster:
domestic, adjective:
1. Living near or about human habitations (tame, domesticated)
2. Of, relating to, or originating with a country and especially
one's own country
3. of or relating to the household or the family
4. devoted to home duties and pleasures
5. indigenous

indigenous, adjective
1. having originated in and being produced, growing, living or
occurring naturally in a particular region or environment

Is that suppose to prove a point?

I'm a native speaker of English, and I know how to speak.

Where I'm from, "domestic" can mean something along the lines of
"ordinary".

Throw five thousand dictionaries at me that say otherwise and it won't
make a blind bit of difference.
--

Frederick Gotham
Jun 28 '06 #32
Frederick Gotham schrieb:
Eric Sosman posted:
(2) One's complement:

There's only one bit-pattern for zero.
One extra unit in the negative range.


There are two bit patterns for zero, and no extra unit.


So what use is One's complement at all? If it still has two values for
zero, and has no extra unit in the negative range, then what possible good
can it do? It seems that Two's complement would be superior to it in every
way.


You did not really read my reply <4g*************@individual.net>
before replying to "domestic", did you?

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 28 '06 #33
pete posted:

I had teachers that talked like that.

"Domestic" is the opposite of the of "exotic".

The primary and secondary meanings of exotic are
"foreign" and "unusual".

"Domestic" being used as the opposite
of the secondary meaning of "exotic", is exotic.

Here's a post I read on comp.lang.c++ today:
http://groups.google.ie/group/comp.l...80661ba?hl=en&
Note its use of "exotic". In Ireland, we make similar arbitrary use of the
word "domestic".

--

Frederick Gotham
Jun 28 '06 #34

pete wrote:
Bill Pursell wrote:

Frederick Gotham wrote:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.


This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?

signed magnitude is taking the top half
of the integers, flipping them over and moving them to the
bottom, with the zeros overlapping. (From this description,
it's far more obvious why 2's complement is simpler.)

One's complement is derived by moving the top half
to the bottom and shifting up by one to make the zeros
overlap.

It feels like those descriptions are not clear to the clueless
newbie, however. I'm not sure they're even clear to anyone
unless they already understand the number system...


I already understand the number system.
I don't understand the following terms:
"top half of the integers"
"moving them to the bottom"


By "top half of the unsigned integers", I mean those
values with the MSB set. I'm picturing the values stacked
up with 00..00 on the bottom and 11..11 on the top and
the number line to the right of the stack, with the number
line aligned in such a way that each bit collection is next
to it's usual interpretation. So negative values and those
above UINT_MAX have nothing to the left of them. Now
grab the top half of the stack and move it down so that
11..11 is to the left of -1. It's a graphic that I can see clearly
in my head, but I don't think I've ever described it accurately
enough to explain it to anyone that didn't already grok. Maybe
it's just a bad description....

Jun 28 '06 #35
Frederick Gotham <fg*******@SPAM.com> writes:
Dik T. Winter posted:
Merriam-Webster:
domestic, adjective:
1. Living near or about human habitations (tame, domesticated)
2. Of, relating to, or originating with a country and especially
one's own country
3. of or relating to the household or the family
4. devoted to home duties and pleasures
5. indigenous

indigenous, adjective
1. having originated in and being produced, growing, living or
occurring naturally in a particular region or environment


Is that suppose to prove a point?

I'm a native speaker of English, and I know how to speak.

Where I'm from, "domestic" can mean something along the lines of
"ordinary".

Throw five thousand dictionaries at me that say otherwise and it won't
make a blind bit of difference.


I don't disagree with any of that.

But if you use "domestic" to mean "ordinary", few people who haven't
read this thread will understand what you mean. If you want to be
understood (which I presume is a goal when you post to Usenet), I
respectfully suggest that you avoid that particular usage -- not
because there's anything wrong with it, but because the vast majority
of us are too ignorant of your particular dialect to understand it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 28 '06 #36
On Wed, 28 Jun 2006 01:30:51 GMT, in comp.lang.c , Frederick Gotham
<fg*******@SPAM.com> wrote:
If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?
Curious. I have /never/ heard this usage of domestic before, except
in highly specialised expressions such as "domestic science "
(cookery). If I wanted to say "normal" I'd say normal.

Hope that helps! : )


Not really !
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 29 '06 #37
Mark McIntyre <ma**********@spamcop.net> writes:
On Wed, 28 Jun 2006 01:30:51 GMT, in comp.lang.c , Frederick Gotham
<fg*******@SPAM.com> wrote:
If I say something like:

What's the domestic way of eating a banana?

Then I mean something along the lines of:

What's the commonplace and prevalent method of eating a banana which
has widespread acceptance and is well-known?


Curious. I have /never/ heard this usage of domestic before, except
in highly specialised expressions such as "domestic science "
(cookery). If I wanted to say "normal" I'd say normal.


In "domestic science", I think "domestic" means "in the home", so it's
not an example of Frederick's usage of "domestic" to mean "ordinary".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 29 '06 #38
On Wed, 28 Jun 2006 13:00:26 GMT, "Dik T. Winter" <Di********@cwi.nl>
wrote:
In article <44***********@mindspring.compf*****@mindspring.com writes:
Frederick Gotham wrote:
...
So what use is One's complement at all?
>
Much as 2sC allows the exact same hardware for addition (and
subtraction) of both signed and unsigned, 1sC uses the same hardware
with only addition of end-around-carry (as already noted) which for
ripple carry (which AFAIK all early machines were; lookahead carry
networks are too costly in discrete logic) is just 3 more gates.
My guess is that the ~ (bitwise complement operator)
was a comparitively fast operation on early cpu's.
Right, because each bit is independent. Basically 1sC gives you _much_
of the advantage of 2sC with less added delay. Today this tradeoff is
different because even several gate delays are like one zillionth of
off-chip (especially memory) times.
And it made multipliers for signed integers much easier. Almost all early
computers were either 1's complement or sign-magnitude.
S&M makes multiply easier. AIR 1sC does not; it requires correction
terms as bad (or slightly worse) than 2sC.

- David.Thompson1 at worldnet.att.net
Jul 10 '06 #39
Bill Pursell wrote:
Frederick Gotham wrote:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.

This has always sort of bugged me. The description of
2's complement above is a nice way to describe the
technique for comprehending the bit pattern used to
represent an integer, but it's not a good way to describe
the system. I think a better description is: take the top
half of the unsigned integers and move them to the bottom.
Unfortunately, I can't find the right wording to explain
that description completely. Any thoughts on expanding
it sufficiently to describe it, without making it too verbose?
I'm a math person, so I just think of it as 2sC(x) -x (mod 256); the
positive residue of x modulo 256. A set of residues generally forms a
ring, so the operations, +, -, * and << work as you would expect in the
ring, which is intuitively mapped to the generally integers within
small ranges.

2s complement addition also supports the following relation:

A+B+C = 2*((A and B) or (A and C) or (B and C)) + (A xor B xor C)

Notice how there are two additions on the left side and one on the
right side. So, assuming that you can make hardware for ands and ors
that is way faster than addition (which is the case in the real world,
since ands and ors are completely bit parallel, while addition
generally is not) you can actually compute two serial adds in nearly
the same speed as a single addition. This can be used for improving
hardware speed by quite a bit.

Another interesting formula worth considering is the following:

average = (a + b) / 2

This came up in some digg story a few weeks ago (in which it was
reported that nearly every binary search implementation fails.) The
problem is that if (a+b) overflows, it will inevitably create a
negative number which, after division by two, will result in a
completely erroneous answer. In twos complement we have this:

a + b = 2*(a & b) + (a ^ b)

(set c = 0 in the formula given above.) So we can compute the average
easily by:

average = (a & b) + ((a ^ b) / 2)

which we can easily see has no overflow problems.

Multiplication only needs "fixing" for double-wide multipliers. (But
this is beyond the people in in c.l.c; At least there seems to be
nobody in this n.g. who have any idea what that is or why it is so
important.) You can think about the fix by imagining that the original
operands were as wide as the indended result by sign extending them
appropriately, then the truncated result will be correct.

Division is obviously not an analogy to modulo inversion, but is rather
just a truncating division. I haven't looked deeply at it, but the way
I would do division would be by iterating a division estimator, which
can be done via table look-ups, which can take negative numbers into
account.

Anyhow, this is why 2s complement is so clearly superior to alternative
methods which don't have useful properties anywhere near as useful.
Fortunately, this has been recognized right from the creation of the
very first microprocessor.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 10 '06 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
5134
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
7
8205
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
4
2122
by: Charles A. Lackman | last post by:
Hello, I have some textboxes that are doing addition: Dim AString As Double AString = Convert.ToDouble(TextBox1.Text) - Convert.To(DoubleTextBox2.Text) TextBox3.Text =...
5
6101
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
11909
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
15
35439
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
7
14022
by: andreas.lydersen | last post by:
Hi! While communicating with a monitoring unit, I get some hex values representing degrees celcius from its probes. The values can be something like '19' or '7d'. To convert it to int, I do the...
11
3703
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
20
5774
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.