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


Groups > comp.lang.c > #18188

Re: difference between ~0U and ~0

From Stephen Sprunk <stephen@sprunk.org>
Newsgroups comp.lang.c
Subject Re: difference between ~0U and ~0
Date 2012-03-03 22:55 -0600
Organization A noiseless patient Spider
Message-ID <jiuskf$v3f$1@dont-email.me> (permalink)
References <6ee3baa7-5370-4fb6-acfe-262f2bc6036e@sc10g2000pbc.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web