# Why doesn't this work as expected?

Discussion in 'C++' started by Angus, Jun 17, 2009.

1. ### AngusGuest

int xx = 5;
double nextx = ~(--xx);

I was expecting -4 - but get -5. How do I change to yield -4?

Angus, Jun 17, 2009

2. ### Tim LoveGuest

Angus <> writes:

> int xx = 5;
> double nextx = ~(--xx);

>I was expecting -4 - but get -5. How do I change to yield -4?

Could you go into a little more detail about why you were expecting
the answer -4? Then I could more easily suggest the kind of change you
really want.

Tim Love, Jun 17, 2009

3. ### AngusGuest

On 17 June, 14:12, (Tim Love) wrote:
> Angus <> writes:
> >    int xx = 5;
> >    double nextx = ~(--xx);
> >I was expecting -4 - but get -5.  How do I change to yield -4?

>
> Could you go into a little more detail about why you were expecting
> the answer -4? Then I could more easily suggest the kind of change you
> really want.

I expected the -- decrement to make 4 and the ~ operator would then
make 4 negative.

Angus, Jun 17, 2009
4. ### Tim LoveGuest

Angus <> writes:

>On 17 June, 14:12, (Tim Love) wrote:
>> Angus <> writes:
>> > =A0 =A0int xx =3D 5;
>> > =A0 =A0double nextx =3D ~(--xx);
>> >I was expecting -4 - but get -5. =A0How do I change to yield -4?

>>
>> Could you go into a little more detail about why you were expecting
>> the answer -4? Then I could more easily suggest the kind of change you
>> really want.

>I expected the -- decrement to make 4 and the ~ operator would then
>make 4 negative.

No. ~ flips the bits of the number, so 000......0100 (which is 4 in binary)
becomes 111...1011, which is how -5 is represented. Why not just use

Tim Love, Jun 17, 2009
5. ### AngusGuest

On 17 June, 14:24, (Tim Love) wrote:
> Angus <> writes:
> >On 17 June, 14:12, (Tim Love) wrote:
> >> Angus <> writes:
> >> > =A0 =A0int xx =3D 5;
> >> > =A0 =A0double nextx =3D ~(--xx);
> >> >I was expecting -4 - but get -5. =A0How do I change to yield -4?

>
> >> Could you go into a little more detail about why you were expecting
> >> the answer -4? Then I could more easily suggest the kind of change you
> >> really want.

> >I expected the -- decrement to make 4 and the ~ operator would then
> >make 4 negative.

>
> No. ~ flips the bits of the number, so 000......0100 (which is 4 in binary)
> becomes 111...1011, which is how -5 is represented. Why not just use
> - instead of ~ ?

I wanted to try to transform x to -x(+1) in one operation.

eg take a positive number and make it negative and add 1

Angus, Jun 17, 2009
6. ### Pascal J. BourguignonGuest

Angus <> writes:

> On 17 June, 14:12, (Tim Love) wrote:
>> Angus <> writes:
>> >    int xx = 5;
>> >    double nextx = ~(--xx);
>> >I was expecting -4 - but get -5.

>>
>> Could you go into a little more detail about why you were expecting
>> the answer -4? Then I could more easily suggest the kind of change you
>> really want.

>
> I expected the -- decrement to make 4 and the ~ operator would then
> make 4 negative.

This depends on how the process represents the negative integers.

Nowadays, most processors use the two-complement representation,
which, by definition, encodes -n as (~n)+1 = ~(n-1).

You may want to use a computer using one-complement, then -n = ~n.
Unfortunately this representation means that there are both distinct
values for +0 and -0, so the hardware to compare to zero, etc, is more
complex.

Another way to encode negative integers is to use sign+magnitude, but
then ~n = (2^w-n-1), which is not too helpful.

>> >How do I change to yield -4?

On a two-complement machine, ~5 = -4

You might be bothered by the precondition, and essential
non-portability it express. Then avoid using ~ on integer! ~ is a
bit operator, it should be applied only on bit fields, not on
integers.

One way to get -4 from 5 is n-9; another is 1-n; etc. Depends on what
you mean. Say what you mean!

--
__Pascal Bourguignon__

Pascal J. Bourguignon, Jun 17, 2009
7. ### Pascal J. BourguignonGuest

Angus <> writes:
> I wanted to try to transform x to -x(+1) in one operation.
>
> eg take a positive number and make it negative and add 1

Then just write one operation: 1-x

--
__Pascal Bourguignon__

Pascal J. Bourguignon, Jun 17, 2009
8. ### James KanzeGuest

On Jun 17, 3:06 pm, Angus <> wrote:
> int xx = 5;
> double nextx = ~(--xx);

> I was expecting -4 - but get -5. How do I change to yield -4?

Why were you expecting -4? The numeric results of bitwise
operators on signed integral types is very implementation
defined: the results (in this case) will depend on the
representation of negative numbers. With 2's complement (the
most frequent), you'll get -4, with 1's complement, -5, and with
signed magnitude, a very large negative number, whose exact
value depends on the size of the signed int. (On a Unisys MCP,
for example, I think you'll get -549755813883, for example.)

In general, using bitwise operators on signed integers is
probably best avoided.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze, Jun 18, 2009
9. ### James KanzeGuest

On Jun 17, 3:32 pm, Angus <> wrote:
> On 17 June, 14:24, (Tim Love) wrote:

> I wanted to try to transform x to -x(+1) in one operation.

As Pascal said, say what you mean.

> eg take a positive number and make it negative and add 1

Your original code also modified the original number. If that's
what you want:
-- xx ;
nextx = -xx ;
otherwise:
nextx = -xx + 1 ;
Anything else is just obfuscation, for no real reason.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze, Jun 18, 2009