Difference Between ~0U And ~0 | C Programming - Coding Forums
- Forums New posts Search forums
- Members Current visitors
Search
Everywhere Threads This forum This thread Search titles only Search Advanced search…- New posts
- Search forums
- Forums
- Archive
- Archive
- C Programming
- Thread starter somenath
- Start date Mar 4, 2012
somenath
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. SStephen Sprunk
Hello All, I am confused with the behaviour of the following program #include<stdio.h> int main(void) { printf("\n~0U = %d",~0U); Click to expand...~0U has type (unsigned int); the appropriate format specifier is %u, not %d.
printf("\n~0 = %d",~0); Click to expand...~0 has type (int), so %d is correct.
printf("\n~0U>>1 = %d",~0U>>1); Click to expand...~0U>>1 has type (unsigned int); the appropriate format specifier is %u, not %d.
printf("\n~0>>1 = %d\n",~0>>1); Click to expand...~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. Click to expand...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. Click to expand...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. Click to expand...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 ? Click to expand...Fix the bugs in your code and you'll see that you're asking the wrong questions. S B
BGB
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. Click to expand...because ~0U is unsigned, but ~0 is signed. '>>' does different things for signed and unsigned values. B
Ben Bacarisse
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. Click to expand...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
Close, but backwards. ~0 is all bit 1. So you will get -1 as a result of ~0 in 2's complement system. Click to expand...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
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. Click to expand...The first method turns x into -x; the second turns -x into x. Remember, only 22 more shopping days before March 32. Post reply
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 QuestionSimilar Threads
A process take input from /proc/<pid>/fd/0, but won't process it | 0 | Oct 29, 2023 |
does 0<(unsigned short)0x8000 hold? | 13 | Mar 27, 2012 |
Beginner at c | 0 | Oct 5, 2023 |
Please help with C programming to save GPS reception data in Raspberry Pi. | 0 | Dec 8, 2022 |
Comparison of Integer and Pointer (that's supposed to be an Integer). Where did I go wrong? | 0 | Nov 19, 2022 |
Command Line Arguments | 0 | Mar 7, 2023 |
Help with finding difference between two bodies of text in order | 0 | Sep 11, 2024 |
C program: memory leak/ segmentation fault/ memory limit exceeded | 0 | Nov 12, 2022 |
Members online
- Ray86
Forum statistics
Threads 474,015 Messages 2,570,294 Members 46,914 Latest member staratomyLatest Threads
- Flutter web
- Started by Jean-Marie
- Yesterday at 11:56 PM
- Strange result using "for(in... " iteration
- Started by jimandy
- Yesterday at 7:38 PM
- Multithreaded Server Under Load: Real-Time Packet Exchange with 5 Clients
- Started by Infinityhost
- Friday at 7:32 PM
- Python and HTML
- Started by HTMLpigeon64
- Friday at 7:29 PM
- Urgent Help Needed: Supporting Education and Family Through Hard Work
- Started by miantsafanirina
- Friday at 8:26 AM
- Accountability intro
- Started by bauble
- Thursday at 9:44 AM
- I want to convert Outlook data files to MBOX manually
- Started by Techykaus24
- Thursday at 8:33 AM
- Gray Scott model in FEniCS
- Started by Lonny Petersen
- Wednesday at 7:13 AM
- Creating elements using a loop in JS.
- Started by montyonthebonty
- Tuesday at 11:58 PM
- Bonjour
- Started by WhiteCube
- Tuesday at 8:34 PM
- Forums
- Archive
- Archive
- C Programming
Từ khóa » C 0 Vs 0u
-
Difference Between 0 And 0u - Stack Overflow
-
What Does The Assignment Of ~0u To A Variable Mean In C++?
-
Zero (0 Or 0U) Is Not Equal To Null. And How To Use That With ... - MSDN
-
Unsigned And Signed 0 - C / C++ - Bytes Developer Community
-
What Is The Meaning Of 1u And 0u In The Following Code? - Reddit
-
Placing Suffix U With Unsigned Variables - Keil Forum - Arm Community
-
Why Doesn't Work (unsigned Int) For The Difference Of Two Integer ...
-
C99 Preprocessor Features Adopted In C++0x - IBM
-
C语言中的0U或1U是什么意思? - CSDN博客
-
What Is The Type Of A Constant In C? - Jim Fisher