Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18187 > unrolled thread

difference between ~0U and ~0

Started bysomenath <pal_somenath@emc.com>
First post2012-03-03 20:03 -0800
Last post2012-03-09 18:52 -0500
Articles 9 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  difference between ~0U and ~0 somenath <pal_somenath@emc.com> - 2012-03-03 20:03 -0800
    Re: difference between ~0U and ~0 Stephen Sprunk <stephen@sprunk.org> - 2012-03-03 22:55 -0600
    Re: difference between ~0U and ~0 BGB <cr88192@hotmail.com> - 2012-03-03 23:01 -0700
    Re: difference between ~0U and ~0 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-04 12:50 +0000
    Re: difference between ~0U and ~0 pete <pfiland@mindspring.com> - 2012-03-08 08:55 -0500
      Re: difference between ~0U and ~0 James Piper <usenet@jamespiper.com> - 2012-03-08 06:52 -0800
        Re: difference between ~0U and ~0 pete <pfiland@mindspring.com> - 2012-03-09 17:53 -0500
          Re: difference between ~0U and ~0 Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 15:11 -0800
            Re: difference between ~0U and ~0 pete <pfiland@mindspring.com> - 2012-03-09 18:52 -0500

#18187 — difference between ~0U and ~0

Fromsomenath <pal_somenath@emc.com>
Date2012-03-03 20:03 -0800
Subjectdifference between ~0U and ~0
Message-ID<6ee3baa7-5370-4fb6-acfe-262f2bc6036e@sc10g2000pbc.googlegroups.com>
Hello All,

I am confused with the behaviour of the following program
#include<stdio.h>

int main(void)
{
    printf("\n~0U = %d",~0U);
    printf("\n~0 = %d",~0);
    printf("\n~0U>>1 = %d",~0U>>1);
    printf("\n~0>>1 = %d\n",~0>>1);
    return 0;
}

==========
Output
=========

~0U = -1
~0 = -1
~0U>>1 = 2147483647
~0>>1 = -1
===========
According to my understanding ~0 is -1.So I will get all bit 1 as a
result of ~0 in 2's complement system.

So the first two line of output saying ~0 is same as ~0U. Then why
there is difference in the output of ~0U>>1 and ~0>>1 ?
please provide some input.

[toc] | [next] | [standalone]


#18188

FromStephen Sprunk <stephen@sprunk.org>
Date2012-03-03 22:55 -0600
Message-ID<jiuskf$v3f$1@dont-email.me>
In reply to#18187
On 03-Mar-12 22:03, somenath wrote:
> Hello All,
> 
> I am confused with the behaviour of the following program
> #include<stdio.h>
> 
> int main(void)
> {
>     printf("\n~0U = %d",~0U);

~0U has type (unsigned int); the appropriate format specifier is %u, not %d.

>     printf("\n~0 = %d",~0);

~0 has type (int), so %d is correct.

>     printf("\n~0U>>1 = %d",~0U>>1);

~0U>>1 has type (unsigned int); the appropriate format specifier is %u,
not %d.

>     printf("\n~0>>1 = %d\n",~0>>1);

~0>>1 has type (int), so %d is correct.

>     return 0;
> }
> 
> ==========
> Output
> =========
> 
> ~0U = -1
> ~0 = -1
> ~0U>>1 = 2147483647
> ~0>>1 = -1
> ===========
> According to my understanding ~0 is -1.

That is true for if the zero's type is signed.  However, "0U" is
unsigned, so the result of the ~ operator cannot be negative.

> So I will get all bit 1 as a result of ~0 in 2's complement system.

That is an implementation detail and will only confuse you at this point.

> So the first two line of output saying ~0 is same as ~0U.

Perhaps on your system, but your code invokes undefined behavior by
using the wrong format specifier, so anything is possible.

> Then why there is difference in the output of ~0U>>1 and ~0>>1 ?

Fix the bugs in your code and you'll see that you're asking the wrong
questions.

S


-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


#18190

FromBGB <cr88192@hotmail.com>
Date2012-03-03 23:01 -0700
Message-ID<jiv0hs$34b$1@news.albasani.net>
In reply to#18187
On 3/3/2012 9:03 PM, somenath wrote:
> Hello All,
>
> I am confused with the behaviour of the following program
> #include<stdio.h>
>
> int main(void)
> {
>      printf("\n~0U = %d",~0U);
>      printf("\n~0 = %d",~0);
>      printf("\n~0U>>1 = %d",~0U>>1);
>      printf("\n~0>>1 = %d\n",~0>>1);
>      return 0;
> }
>
> ==========
> Output
> =========
>
> ~0U = -1
> ~0 = -1
> ~0U>>1 = 2147483647
> ~0>>1 = -1
> ===========
> According to my understanding ~0 is -1.So I will get all bit 1 as a
> result of ~0 in 2's complement system.
>
> So the first two line of output saying ~0 is same as ~0U. Then why
> there is difference in the output of ~0U>>1 and ~0>>1 ?
> please provide some input.

because ~0U is unsigned, but ~0 is signed.
'>>' does different things for signed and unsigned values.

[toc] | [prev] | [next] | [standalone]


#18198

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-03-04 12:50 +0000
Message-ID<0.dfe3cb25ac5471e7ee65.20120304125039GMT.87sjhor0m8.fsf@bsb.me.uk>
In reply to#18187
somenath <pal_somenath@emc.com> writes:

> I am confused with the behaviour of the following program
> #include<stdio.h>
>
> int main(void)
> {
>     printf("\n~0U = %d",~0U);
>     printf("\n~0 = %d",~0);
>     printf("\n~0U>>1 = %d",~0U>>1);
>     printf("\n~0>>1 = %d\n",~0>>1);
>     return 0;
> }
>
> ==========
> Output
> =========
>
> ~0U = -1
> ~0 = -1
> ~0U>>1 = 2147483647
> ~0>>1 = -1
> ===========
> According to my understanding ~0 is -1.So I will get all bit 1 as a
> result of ~0 in 2's complement system.
>
> So the first two line of output saying ~0 is same as ~0U. Then why
> there is difference in the output of ~0U>>1 and ~0>>1 ?
> please provide some input.

I don't think anyone has said yet that the result of right shifting a
negative value is, explicitly, implementation-defined.  Some machines
will do one things and some another.  The C standard leaves it up to the
implementation to say what it does.  The result of ~0U >> 1 is defined
by the language -- it must be UINT_MAX/2 (that's C's division
there, of course, not mathematical division) but ~0 >> 1 can be anything
the implementation chooses.  The two most likely possibilities are
INT_MAX and -1.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#18327

Frompete <pfiland@mindspring.com>
Date2012-03-08 08:55 -0500
Message-ID<4F58BA48.527D@mindspring.com>
In reply to#18187
somenath wrote:

> According to my understanding ~0 is -1.
> So I will get all bit 1 as a
> result of ~0 in 2's complement system.

Close, but backwards.

    ~0 is all bit 1.

    So you will get -1 as a
    result of ~0 in 2's complement system.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#18328

FromJames Piper <usenet@jamespiper.com>
Date2012-03-08 06:52 -0800
Message-ID<24006453.364.1331218377093.JavaMail.geo-discussion-forums@vbcu2>
In reply to#18327
On Thursday, March 8, 2012 8:55:20 AM UTC-5, pete wrote:
> somenath wrote:
> 
> > According to my understanding ~0 is -1.
> > So I will get all bit 1 as a
> > result of ~0 in 2's complement system.
> 
> Close, but backwards.
> 
>     ~0 is all bit 1.
> 
>     So you will get -1 as a
>     result of ~0 in 2's complement system.
> 
> -- 
> pete

That's true.

1 in 8-bit format is: 0000 0001
To get neg- 1: 
1. Negate bits. 1111 1110
2. Add 1: 1111 11111

Same results regardless of the bit size.

[toc] | [prev] | [next] | [standalone]


#18415

Frompete <pfiland@mindspring.com>
Date2012-03-09 17:53 -0500
Message-ID<4F5A8A03.3657@mindspring.com>
In reply to#18328
James Piper wrote:
> 
> On Thursday, March 8, 2012 8:55:20 AM UTC-5, pete wrote:
> > somenath wrote:
> >
> > > According to my understanding ~0 is -1.
> > > So I will get all bit 1 as a
> > > result of ~0 in 2's complement system.
> >
> > Close, but backwards.
> >
> >     ~0 is all bit 1.
> >
> >     So you will get -1 as a
> >     result of ~0 in 2's complement system.
> That's true.
> 
> 1 in 8-bit format is: 0000 0001
> To get neg- 1:
> 1. Negate bits. 1111 1110
> 2. Add 1: 1111 11111
> 
> Same results regardless of the bit size.

Another way for negatising in two's complement is:
1. subtract 1. 0000 0000
2. Negate bits: 1111 11111

I've worked on assembly code 
that had it done either way in different parts of the code.
I don't know if it was done by the same person.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#18418

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-03-09 15:11 -0800
Message-ID<kfn1up1pdxj.fsf@x-alumni2.alumni.caltech.edu>
In reply to#18415
pete <pfiland@mindspring.com> writes:

> James Piper wrote:
>> 
>> On Thursday, March 8, 2012 8:55:20 AM UTC-5, pete wrote:
>> > somenath wrote:
>> >
>> > > According to my understanding ~0 is -1.
>> > > So I will get all bit 1 as a
>> > > result of ~0 in 2's complement system.
>> >
>> > Close, but backwards.
>> >
>> >     ~0 is all bit 1.
>> >
>> >     So you will get -1 as a
>> >     result of ~0 in 2's complement system.
>> That's true.
>> 
>> 1 in 8-bit format is: 0000 0001
>> To get neg- 1:
>> 1. Negate bits. 1111 1110
>> 2. Add 1: 1111 11111
>> 
>> Same results regardless of the bit size.
>
> Another way for negatising in two's complement is:
> 1. subtract 1. 0000 0000
> 2. Negate bits: 1111 11111
>
> I've worked on assembly code 
> that had it done either way in different parts of the code.
> I don't know if it was done by the same person.

The first method turns x into -x;  the second turns -x into x.

Remember, only 22 more shopping days before March 32.

[toc] | [prev] | [next] | [standalone]


#18421

Frompete <pfiland@mindspring.com>
Date2012-03-09 18:52 -0500
Message-ID<4F5A97A8.7D9B@mindspring.com>
In reply to#18418
Tim Rentsch wrote:
> 
> pete <pfiland@mindspring.com> writes:
> 
> > James Piper wrote:
> >>
> >> On Thursday, March 8, 2012 8:55:20 AM UTC-5, pete wrote:
> >> > somenath wrote:
> >> >
> >> > > According to my understanding ~0 is -1.
> >> > > So I will get all bit 1 as a
> >> > > result of ~0 in 2's complement system.
> >> >
> >> > Close, but backwards.
> >> >
> >> >     ~0 is all bit 1.
> >> >
> >> >     So you will get -1 as a
> >> >     result of ~0 in 2's complement system.
> >> That's true.
> >>
> >> 1 in 8-bit format is: 0000 0001
> >> To get neg- 1:
> >> 1. Negate bits. 1111 1110
> >> 2. Add 1: 1111 11111
> >>
> >> Same results regardless of the bit size.
> >
> > Another way for negatising in two's complement is:
> > 1. subtract 1. 0000 0000
> > 2. Negate bits: 1111 11111
> >
> > I've worked on assembly code
> > that had it done either way in different parts of the code.
> > I don't know if it was done by the same person.
> 
> The first method turns x into -x;  the second turns -x into x.

I'm not going to dignify that with a response.

Wait, I told that wrong ...

-- 
pete

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web