473,378 Members | 1,360 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,378 software developers and data experts.

ranges of unsigned int/long

Hi,

I was writing a testing program to test the ranges of char, short, int
and long variables on my computer, both signed and unsigned.

Everything was fine except for unsigned int and unsigned long. I got
0 to -1 for both. The expected answers should be:

unsigned int: 0 to 65535
unsigned long: 0 to 4294967295

What might be wrong here? Please help.

Here is the program I wrote:

-------------------

#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("Unsigned char: %d\t%d\n", 0, UCHAR_MAX);
printf("Signed char: %d\t%d\n", SCHAR_MIN, SCHAR_MAX);
printf("Unsigned short: %d\t%d\n", 0, USHRT_MAX);
printf("(Signed) short: %d\t%d\n", SHRT_MIN, SHRT_MAX);
printf("Unsigned int: %d\t%d\n", 0, UINT_MAX);
printf("(Signed) int: %d\t%d\n", INT_MIN, INT_MAX);
printf("Unsigned long: %d\t%d\n", 0, ULONG_MAX);
printf("(Signed) long: %d\t%d\n", LONG_MIN, LONG_MAX);
getchar();
return 0;
}

-----------------
Jan 17 '07 #1
3 13155
Nicholas Zhou wrote:
Hi,

I was writing a testing program to test the ranges of char, short, int
and long variables on my computer, both signed and unsigned.

Everything was fine except for unsigned int and unsigned long. I got
0 to -1 for both. The expected answers should be:

unsigned int: 0 to 65535
unsigned long: 0 to 4294967295

What might be wrong here? Please help.
You used the wrong conversion specifiers; i.e. for unsigned int, use %u,
long, use %ld, etc.)

--
Clark S. Cox III
cl*******@gmail.com
Jan 17 '07 #2
#include <limits.h>
#include <stdio.h>
int main(void)
{
printf("Signed char : %20d\t%20d\n", SCHAR_MIN, SCHAR_MAX);
printf("Signed short : %20d\t%20d\n", SHRT_MIN, SHRT_MAX);
printf("Signed int : %20d\t%20d\n", INT_MIN, INT_MAX);
printf("Signed long : %20ld\t%20ld\n", LONG_MIN, LONG_MAX);
printf("Signed long long : %20lld\t%20lld\n", LLONG_MIN,
LLONG_MAX);
printf("Unsigned char : %20d\t%20u\n", 0, (unsigned)
UCHAR_MAX);
printf("Unsigned short : %20d\t%20u\n", 0,
(unsigned)USHRT_MAX);
printf("Unsigned int : %20d\t%20u\n", 0, (unsigned)UINT_MAX);
printf("Unsigned long : %20d\t%20lu\n", 0, (unsigned
long)ULONG_MAX);
printf("Unsigned long long : %20d\t%20llu\n", 0, (unsigned long
long) ULLONG_MAX);
return 0;
}
/* One possible output:
C:\tmp>foo
Signed char : -128 127
Signed short : -32768 32767
Signed int : -2147483648 2147483647
Signed long : -2147483648 2147483647
Signed long long : -9223372036854775808 9223372036854775807
Unsigned char : 0 255
Unsigned short : 0 65535
Unsigned int : 0 4294967295
Unsigned long : 0 4294967295
Unsigned long long : 0 18446744073709551615
*/

Jan 17 '07 #3
"Nicholas Zhou" <zh******@osu.eduwrote in message
news:eo**********@charm.magnus.acs.ohio-state.edu...
Hi,

I was writing a testing program to test the ranges of char, short, int and
long variables on my computer, both signed and unsigned.

Everything was fine except for unsigned int and unsigned long. I got
0 to -1 for both. The expected answers should be:

unsigned int: 0 to 65535
unsigned long: 0 to 4294967295

What might be wrong here? Please help.
Assuming a standard 2's complement machine (just about all of them nowadays,
I think), the reason you got -1 is that the bit pattern of all 1's in an
integer data type corresponds to:

a)The largest possible positive value, if the data is interpreted as an
unsigned type.

b)-1, if the value is interpreted as a signed type.

As other posters pointed out, the problem was the format specifier. The
format specifier caused interpretation as a signed type.

An integer in memory is just a collection of 0's and 1's. It can be either
unsigned or signed. It is all in how you interpret it.

2's complement has historically been used in computers because the same
addition and subtraction instructions give correct results for both unsigned
and signed interpretations. However, a little extra digital logic and a
couple of extra flags are required in the processor for correct branches and
so on.

The smallest practical example is 3 bits. Here are the values when
interpreted as unsigned and signed.

Bit Pattern Unsigned Signed

000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1

Notice that if N is the number of bits, the largest value corresponds to
2^N-1 as an unsigned or -1 as a signed.

This is all explained (I hope) here:

http://en.wikipedia.org/wiki/Twos_complement

One other thing you might notice is that the representations correspond to
the same values up until the sign bit is set. It is a not-uncommon problem
in 'C' to introduce a bug that only becomes apparent at large data values
because one somehow casts an unsigned to a signed. Everything works great.
Until 2^(N-1). Then all hell breaks loose.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
30
by: Alf P. Steinbach | last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5> mentions that <quote> C++ guarantees a char is exactly one...
25
by: amit | last post by:
Hi, What will happen in following scenario.. long a = -2; longlong b = a long c; c = *(ulonglong *)&b; b = c; also when
9
by: Christoph Bartoschek | last post by:
Hi, some time ago I've read a paper that advised to store intervals on discrete values in the format [a;b[. One starts with the first element and finishes with one past the last one. However...
14
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
11
by: Masud | last post by:
hi, for a test i want to generate different random numbers between different ranges in a single loop. I tried to solve in the following way but it always profile same value. I am looking for...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.