Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > difference between ~0U and ~0

Reply
Thread Tools

difference between ~0U and ~0

 
 
somenath
Guest
Posts: n/a
 
      03-04-2012
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.
 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      03-04-2012
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
 
Reply With Quote
 
 
 
 
BGB
Guest
Posts: n/a
 
      03-04-2012
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.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-04-2012
somenath <(E-Mail Removed)> 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.
 
Reply With Quote
 
James Piper
Guest
Posts: n/a
 
      03-08-2012
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.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      03-09-2012
pete <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments