473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is the representation of a signed number in unsigned form ?

Expand|Select|Wrap|Line Numbers
  1. main()
  2.  
  3. {
  4.  
  5. unsigned i=3;
  6.  
  7. if(i>-1)
  8.  
  9.     printf("Hell");
  10.  
  11. else
  12.  
  13.    printf("Heaven");
  14.  
  15. }
why is the output Heaven ,plz explain the logic for this .
Jul 18 '15 #1
3 1638
weaknessforcats
9,208 Expert Mod 8TB
Usually, the leftmost bit of the signed int is the sign bit. In unsigned form, this bit becomes part of the value.

Also, negative signed ints are usually in in 2's complement form so they would appear as very large negative numbers.
Jul 18 '15 #2
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. main() {
  2.  
  3. unsigned i=3;
  4.  
  5. printf("i= %u\n", i);
  6. printf("i= %u\n", -1);
  7.  
  8. if( i > -1 )
  9.         printf("Hell");
  10.     else
  11.         printf("Heaven");
  12. }
  13.  
check: this "In i > -1, the -1 is converted to unsigned int, resulting in the value UINT_MAX. i is never bigger than that value, so the loop body never executes."
Jul 18 '15 #3
donbock
2,426 Expert 2GB
Line 7 of the OP compares i to -1. i is an unsigned int, -1 is a signed int. They are both flavors of int, so they have the same rank. One of the rules for the Usual Arithmetic Conversions is "if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type." Thus, -1 gets converted from int to unsigned int before the comparison takes place.

C99 mandates that C only permit three possible encoding techniques for integer values:
  • 2's-complement
  • 1's-complement
  • Sign-magnitude
If we assume ints have 16 bits, then
Expand|Select|Wrap|Line Numbers
  1. Technique      (unsigned int)3   (int)(-1) (unsigned int)(-1)
  2. 2's-complement     0x0003         0xFFFF      65535  
  3. 1's-complement     0x0003         0xFFFE      65534
  4. sign-magnitude     0x0003         0x8001      32769
  5.  
In every case, casting -1 to an unsigned int yields a value greater than 3.
Jul 22 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one...
2
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
5
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
22
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
3
by: Bilgehan.Balban | last post by:
Hi, I have a basic question on signed and unsigned integers. Consider the following code: #define SOME_ADDR 0x10000000 // Some context { unsigned int *x = (unsigned int *)(SOME_ADDR);
26
by: LuB | last post by:
This isn't a C++ question per se ... but rather, I'm posting this bcs I want the answer from a C++ language perspective. Hope that makes sense. I was reading Peter van der Linden's "Expert C...
18
by: Timothee Groleau | last post by:
Hi all, When should I be worried about doing a comparison of signed vs unsigned ints? Could someone give me a example where such a comparison would lead to unwanted results? Thanks, Tim.
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
4
by: At_sea_with_C | last post by:
hello all, Is there a reason to prefer char over signed or unsigned char. From waht I know all strings can be coded with unsigned char while a negative number can be put in signed char. So what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.