Why doesn't this work as expected?

A

Angus

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

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

Tim Love

Angus said:
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.
 
A

Angus

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.
 
T

Tim Love

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 ~ ?
 
A

Angus

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
 
P

Pascal J. Bourguignon

Angus said:
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.


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!
 
P

Pascal J. Bourguignon

Angus said:
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
 
J

James Kanze

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.
 
J

James Kanze

On 17 June, 14:24, (e-mail address removed) (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top