Why doesn't this work as expected?

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

  1. Angus

    Angus Guest

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

    I was expecting -4 - but get -5. How do I change to yield -4?
     
    Angus, Jun 17, 2009
    #1
    1. Advertising

  2. Angus

    Tim Love Guest

    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
    #2
    1. Advertising

  3. Angus

    Angus Guest

    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
    #3
  4. Angus

    Tim Love Guest

    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 ~ ?
     
    Tim Love, Jun 17, 2009
    #4
  5. Angus

    Angus Guest

    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
    #5
  6. 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
    #6
  7. 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
    #7
  8. Angus

    James Kanze Guest

    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
    #8
  9. Angus

    James Kanze Guest

    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
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Patrick
    Replies:
    2
    Views:
    3,647
    Keith Patrick
    Nov 16, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  3. Alex Buell
    Replies:
    6
    Views:
    316
    Alex Buell
    Feb 23, 2006
  4. thomas
    Replies:
    23
    Views:
    1,926
    Andrey Tarasevich
    Feb 22, 2008
  5. Mc Lauren Series
    Replies:
    11
    Views:
    955
    BootNic
    Feb 12, 2010
Loading...

Share This Page