473,409 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

one's compliment operator


Hi,

when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3

Thanks,
Deepak
Jul 16 '08 #1
10 4871
>when I execute following program which find's one's compliment of 2,
>I'm getting -3 where
That output is correct, for a two's complement machine.
>I was expecting -2.
Take that bit pattern over to a one's complement machine, and it
will print -2 there. The same bit pattern on a two's complement
machine is -3.
>Is there anything fundamentally wrong in my
understanding?
The one's complement is one less than the two's complement.
On a two's complement machine (close to all machines are,
especially modern ones), the two's complement of N is
displayed as -N, and the one's complement of N is -N-1.

For example, the two's complement of 0 is 0, and the
one's complement of 0 is -1.
>main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3
Jul 16 '08 #2
On Jul 16, 12:37 pm, gordonb.hp...@burditt.org (Gordon Burditt) wrote:
when I execute following program which find's one's compliment of 2,
I'm getting -3 where

That output is correct, for a two's complement machine.
I was expecting -2.

Take that bit pattern over to a one's complement machine, and it
will print -2 there. The same bit pattern on a two's complement
machine is -3.
Is there anything fundamentally wrong in my
understanding?

The one's complement is one less than the two's complement.
On a two's complement machine (close to all machines are,
especially modern ones), the two's complement of N is
displayed as -N, and the one's complement of N is -N-1.
What machine is going to gain by changing it to 2's complement from
1's complement?
For example, the two's complement of 0 is 0, and the
one's complement of 0 is -1.
main()
{
int a = 2;
printf("%d\n", ~a);
}
O/P:
-3
Jul 16 '08 #3
deepak wrote:
Hi,

when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
Yes, there is.
>

main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3
Examine closely the output of the following code and try to see what's
happening. Note that the code below, unlike yours, properly specifies
the headers included, properly indicates the return type for main, and
properly returns a value from main (and one with a defined meaning).

#include <stdio.h>
#include <string.h>

int main(void)
{
/* There are purposeful inefficiencies below for the sake of clarity */
unsigned int x;
signed int y, y2;
for (y = 10; y >= -10; y--) {
memcpy(&x, &y, sizeof x);
printf("y = %3d, x = %#0o | ", y, x);
x = ~x;
memcpy(&y2, &x, sizeof x);
printf("~x = %#0o, y2 = %d\n", x, y2);
}
return 0;
}
y = 10, x = 012 | ~x = 037777777765, y2 = -11
y = 9, x = 011 | ~x = 037777777766, y2 = -10
y = 8, x = 010 | ~x = 037777777767, y2 = -9
y = 7, x = 07 | ~x = 037777777770, y2 = -8
y = 6, x = 06 | ~x = 037777777771, y2 = -7
y = 5, x = 05 | ~x = 037777777772, y2 = -6
y = 4, x = 04 | ~x = 037777777773, y2 = -5
y = 3, x = 03 | ~x = 037777777774, y2 = -4
y = 2, x = 02 | ~x = 037777777775, y2 = -3
y = 1, x = 01 | ~x = 037777777776, y2 = -2
y = 0, x = 0 | ~x = 037777777777, y2 = -1
y = -1, x = 037777777777 | ~x = 0, y2 = 0
y = -2, x = 037777777776 | ~x = 01, y2 = 1
y = -3, x = 037777777775 | ~x = 02, y2 = 2
y = -4, x = 037777777774 | ~x = 03, y2 = 3
y = -5, x = 037777777773 | ~x = 04, y2 = 4
y = -6, x = 037777777772 | ~x = 05, y2 = 5
y = -7, x = 037777777771 | ~x = 06, y2 = 6
y = -8, x = 037777777770 | ~x = 07, y2 = 7
y = -9, x = 037777777767 | ~x = 010, y2 = 8
y = -10, x = 037777777766 | ~x = 011, y2 = 9
Jul 16 '08 #4
"deepak" writes:
On Jul 16, 12:37 pm, gordonb.hp...@burditt.org (Gordon Burditt) wrote:
>when I execute following program which find's one's compliment of 2,
I'm getting -3 where

That output is correct, for a two's complement machine.
>I was expecting -2.

Take that bit pattern over to a one's complement machine, and it
will print -2 there. The same bit pattern on a two's complement
machine is -3.
>Is there anything fundamentally wrong in my
understanding?

The one's complement is one less than the two's complement.
On a two's complement machine (close to all machines are,
especially modern ones), the two's complement of N is
displayed as -N, and the one's complement of N is -N-1.
What machine is going to gain by changing it to 2's complement from
1's complement?
The first step is to identify what "it" is. Is "it" a computer? The design
of a computer? A number? All of the above? None of the above?
Jul 16 '08 #5
deepak <de*********@gmail.comwrites:
[...]
What machine is going to gain by changing it to 2's complement from
1's complement?
[...]

I'm having trouble understanding your question, but I'll try to answer
it anyway.

Representing unsigned integers is straightforward: each bit represents
a power of two. (There are complications, but we'll ignore them for
now). Representing signed integers is a bit more tricky.

There are three major ways to represent signed integers:

sign-and-magnitude simply reserves a bit to indicate the sign; this
bit is set to 0 for positive numbers, 1 for negative numbers. To
negate a number, just flip the sign bit.

In ones' complement, you negate a number by flipping *all* the bits.

In two's complement, you negate a number by flipping all the bits and
then adding 1.

It turns out that two's complement has considerable advantages over
the other representations. For one thing, it doesn't have two
different representations for zero; if you negate 0, you just get 0
again. The other representations both have distinct representations
for +0 and -0, which means you have to decide how to deal with them.
(Are +0 and -0 equal? If so, you can't compare two numbers just by
comparing all the bits.) Also, if you ignore overflow/wraparound,
many signed and unsigned operations are identical.

For this and other reasons, almost all machines these days use two's
complement for signed integers; the other representations aren't quite
dead, but they're rare.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '08 #6
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>It turns out that two's complement has considerable advantages over
the other representations. For one thing, it doesn't have two
different representations for zero; if you negate 0, you just get 0
again. The other representations both have distinct representations
for +0 and -0, which means you have to decide how to deal with them.
(Are +0 and -0 equal? If so, you can't compare two numbers just by
comparing all the bits.) Also, if you ignore overflow/wraparound,
many signed and unsigned operations are identical.
The most compelling reason is that it's just what you get if you apply
the usual algorithms for positive numbers even when the result might
be negative. You don't need to do anything special about negative
numbers at all.

Try it in base ten: subtract one from zero. 0 - 1 you can't do, so borrow
10. 10 - 1 is 9. Continue for however many digits you're using, and you'll
find 0 - 1 is 999...999. You're using ten's complement.

So to turn Keith's last sentence around, it's not that you find that
signed and unsigned operations are identical, it's that when you apply
operations without considering sign you find that you have invented a
consistent way of representing negative numbers.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Jul 16 '08 #7
deepak wrote:
Hi,

when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3
What's fundamentally wrong with your understanding is your expectation
that the '~' operator would give you the ones' complEment of it's
operand. That's not how it's defined. The correct definition is in
6.5.3.3p4:

"The result of the ~ operator is the bitwise complement of its
(promoted) operand (that is,
each bit in the result is set if and only if the corresponding bit in
the converted operand is
not set)."

That is the same as the ones' complement only when the promoted type
of the operand is represented on that machine using one's complement
notation. Since machines using one's complement notation are extremely
rare nowadays, you're far more likely to generate the twos' complement
when you apply the '~' operator.
Jul 16 '08 #8
ja*********@verizon.net writes:
deepak wrote:
>when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3

What's fundamentally wrong with your understanding is your expectation
that the '~' operator would give you the ones' complEment of it's
operand. That's not how it's defined. The correct definition is in
6.5.3.3p4:

"The result of the ~ operator is the bitwise complement of its
(promoted) operand (that is,
each bit in the result is set if and only if the corresponding bit in
the converted operand is
not set)."

That is the same as the ones' complement only when the promoted type
of the operand is represented on that machine using one's complement
notation. Since machines using one's complement notation are extremely
rare nowadays, you're far more likely to generate the twos' complement
when you apply the '~' operator.
My understanding of what "the ones' complement" of a number means
differs from yours.

Here's how I'd say it:

The ~ operator does yield the ones' complement of its operand. This
won't be the *negated* value of the operand unless the system happens
to use a ones' complement representation for signed integers.

On a two's complement machine, the ~ operator still yields the ones'
complement of its operand. If it yielded the two's complement, as you
said above, it would be a negation operator; it isn't.

Though I probably wouldn't refer to ones' complement as an operation.
The operation in question is the bitwise complement. "Ones'
complement" usually refers to the particular representation that uses
the bitwise complement operator for arithmetic negation.

If you want the negated value of something just use the unary "-"
operator.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '08 #9
Keith Thompson wrote:
ja*********@verizon.net writes:
deepak wrote:
when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
int a = 2;

printf("%d\n", ~a);
}

O/P:
-3
What's fundamentally wrong with your understanding is your expectation
that the '~' operator would give you the ones' complEment of it's
operand. That's not how it's defined. The correct definition is in
6.5.3.3p4:

"The result of the ~ operator is the bitwise complement of its
(promoted) operand (that is,
each bit in the result is set if and only if the corresponding bit in
the converted operand is
not set)."

That is the same as the ones' complement only when the promoted type
of the operand is represented on that machine using one's complement
notation. Since machines using one's complement notation are extremely
rare nowadays, you're far more likely to generate the twos' complement
when you apply the '~' operator.

My understanding of what "the ones' complement" of a number means
differs from yours.
To be honest, I have no such understanding of my own; I was making a
guess as to what the term meant to deepak. I'm familiar with the term
only as the name for a method of representing numbers, not as the name
for an operation that could be performed on them.

The one thing that I was clear about is that what he called the ones'
complement of a number differs from what the standard requires for the
result of applying the '~' operator on a value whose promoted type has
a twos' complement representation.

....
Though I probably wouldn't refer to ones' complement as an operation.
The operation in question is the bitwise complement. "Ones'
complement" usually refers to the particular representation that uses
the bitwise complement operator for arithmetic negation.
So we're pretty much in agreement.
Jul 16 '08 #10
>when I execute following program which find's one's compliment of 2,
>I'm getting -3 where

That output is correct, for a two's complement machine.
>I was expecting -2.

Take that bit pattern over to a one's complement machine, and it
will print -2 there. The same bit pattern on a two's complement
machine is -3.
>Is there anything fundamentally wrong in my
understanding?

The one's complement is one less than the two's complement.
On a two's complement machine (close to all machines are,
especially modern ones), the two's complement of N is
displayed as -N, and the one's complement of N is -N-1.
What machine is going to gain by changing it to 2's complement from
1's complement?
It doesn't *change* anything. It interprets it as though it were
2's complement (or whatever the native complement is). If you
printf() something using a signed integer conversion, it interprets
it as though it were the native complement (in this case giving
-3). If you printf() the same thing using an unsigned integer
conversion, it interprets it as though it were unsigned (so it might
print 65533 on a machine with 16-bit ints). If you printf() something
using a %lf conversion, it interprets it as though it were a double,
which may (and likely will) have a drastically different value than
a long integer you passed in, even if there wasn't an issue of
doubles and longs possibly being different sizes.

Note that there's no little tag carried with the number saying "this
is in 1's complement". What you get is raw bits. C doesn't have
runtime-variable types, and in any case, there's no type for
explicitly-1's-complement integers.

The primary use of the complement operator is not to calculate stuff
in 1's complement. The primary use is as a bitwise NOT operator,
and it's commonly used along with the & and | bitwise operators.

Jul 16 '08 #11

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

Similar topics

19
by: Neal | last post by:
I can have a visitor choose a subject line for an email. <div><label for="subject">Subject:</label><br /> <select class="form" id="subject" name="subject"> <option value="one">One</option>...
16
by: Emanuel Ziegler | last post by:
Hi, I am using the vector class from the STL. Normally, I initialize values element by element, which is very uncomfortable and sometimes impossible (e.g. when passing a constant vector to an...
19
by: Harshan | last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!) I came to know that it uses 2's compliment...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
33
by: Ruffin Bailey | last post by:
I coulda sworn I was given an explanation during an AppDev class years ago for VB6, but don't recall the answer. Why is it that -1 is True in Visual Basic (and now VB.NET)? Bit flags seem like...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
2
by: Kavya | last post by:
Here is the code ------------------------ class circle{ private: int radius; public: circle(int r=0){ radius=r; } };
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
by: watches0898 | last post by:
Edwards Garments Company is one popular industry specific designer specializing in work wear that ranges from chef coats to chef hats to separates to housecleaning uniforms. Edwards Garments...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.