difference between ~0U and ~0 | C Programming (2024)

  • Forums
  • Archive
  • Archive
  • C Programming
  • Thread startersomenath
  • Start dateMar 4, 2012

S

somenath

  • Mar 4, 2012
  • #1

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.

S

Stephen Sprunk

  • Mar 4, 2012
  • #2

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.

~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

B

BGB

  • Mar 4, 2012
  • #3

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.

B

Ben Bacarisse

  • Mar 4, 2012
  • #4

somenath said:

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.

J

James Piper

  • Mar 8, 2012
  • #5

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.

T

Tim Rentsch

  • Mar 9, 2012
  • #6

pete said:

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.

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

Similar Threads

A process take input from /proc/<pid>/fd/0, but won't process it0
does 0<(unsigned short)0x8000 hold?13
Beginner at c0
Please help with C programming to save GPS reception data in Raspberry Pi.0
Comparison of Integer and Pointer (that's supposed to be an Integer). Where did I go wrong?0
Command Line Arguments0
C program: memory leak/ segmentation fault/ memory limit exceeded0
difference between pointers44

Members online

No members online now.

Total:124 (members:0, guests:124)
Robots:67

Forum statistics

Threads
473,876
Messages
2,569,932
Members
46,211
Latest member
LesLoar128

Latest Threads

  • C# problem

    • Started by Ashley
  • Need Graphic Designer

    • Started by benparker431
  • Am I admin or not?

    • Started by IBMJunkman
  • Can this be made to work?

    • Started by jimandy
  • Seeking co-founders for my company.

    • Started by najasnake12
  • Kia Ora, How's Everyone Doin'?

    • Started by A.N.Omalum
  • Starting a business

    • Started by najasnake12
  • Starting a company

    • Started by najasnake12
  • I want to make a widget for Windows, with keyboard-access

    • Started by AudunNilsen
  • How to sort/filter IG posts by comments number on others posts?

    • Started by Happydz
  • Forums
  • Archive
  • Archive
  • C Programming
difference between ~0U and ~0 | C Programming (2024)
Top Articles
What Responsibilities Are Listed In Duties 2 3 And 4
Ebaniebridges Leak
Katie Nickolaou Leaving
Joi Databas
Elleypoint
Ffxiv Shelfeye Reaver
The Daily News Leader from Staunton, Virginia
Paris 2024: Kellie Harrington has 'no more mountains' as double Olympic champion retires
Txtvrfy Sheridan Wy
According To The Wall Street Journal Weegy
Gw2 Legendary Amulet
Sunday World Northern Ireland
Matthew Rotuno Johnson
Explore Top Free Tattoo Fonts: Style Your Ink Perfectly! 🖌️
Aldi Sign In Careers
Amc Flight Schedule
St Maries Idaho Craigslist
Www Craigslist Milwaukee Wi
Why Should We Hire You? - Professional Answers for 2024
Ahrefs Koopje
Long Island Jobs Craigslist
Aerocareusa Hmebillpay Com
Ups Drop Off Newton Ks
Rimworld Prison Break
Craigslist Org Appleton Wi
Craigslist Apartments Baltimore
At&T Outage Today 2022 Map
The Many Faces of the Craigslist Killer
Southland Goldendoodles
Crossword Help - Find Missing Letters & Solve Clues
Turns As A Jetliner Crossword Clue
Superhot Free Online Game Unblocked
Core Relief Texas
Possum Exam Fallout 76
Vadoc Gtlvisitme App
A Small Traveling Suitcase Figgerits
Police Academy Butler Tech
Ljw Obits
RALEY MEDICAL | Oklahoma Department of Rehabilitation Services
Winco Money Order Hours
Doordash Promo Code Generator
St Anthony Hospital Crown Point Visiting Hours
Samantha Lyne Wikipedia
814-747-6702
Quick Base Dcps
Brother Bear Tattoo Ideas
Beds From Rent-A-Center
Das schönste Comeback des Jahres: Warum die Vengaboys nie wieder gehen dürfen
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Grace Family Church Land O Lakes
Runelite Ground Markers
Nkey rollover - Hitta bästa priset på Prisjakt
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5588

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.