LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2009, 06:06 PM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Question unsigned integers in C


From what I understand, if I attempt to increment an unsigned int which is already at its max value, it will flip back to zero.

Question: is this guaranteed to happen by the C language? That is, can I count on this happening on all platforms?

(Of course, I recognize that the max value of the int will change from platform to platform...)
 
Old 03-24-2009, 06:10 PM   #2
khodeir
Member
 
Registered: Feb 2009
Distribution: Debian
Posts: 243

Rep: Reputation: 33
Quote:
Originally Posted by CoderMan View Post
From what I understand, if I attempt to increment an unsigned int which is already at its max value, it will flip back to zero.

Question: is this guaranteed to happen by the C language? That is, can I count on this happening on all platforms?

(Of course, I recognize that the max value of the int will change from platform to platform...)
when you exceeds the limit the register overflows this will happen on any operating system try factorial 444444
you must take care when you make a program
try using float number because it has 8 or 10 bytes as i remembered and this is a greet resolution
if you want more : there are libraries for encryption that has 1000 bit as i think that will make all things large
 
Old 03-24-2009, 06:15 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think it is splitting hairs as to whether it is C or the hardware. I believe this is called overflow---Imagine an 8-bit register containing 11111111 (hex FF, dec 255). Add 1, and it tries to go to 100000000, but that "1" is the ninth bit, which--as they say--falls on the floor. With an 8 bit register, you don't know if your count is at 0 or 256 (or 512,768, etc.)

The header files define exactly what different data types are on various HW platforms.
 
Old 03-24-2009, 06:31 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by CoderMan View Post
is this guaranteed to happen by the C language?
I don't know. A surprisingly large number of similar basic behaviors in boundary conditions are NOT guaranteed by the language standard (there also may be a question of which version of the language standard).

Quote:
That is, can I count on this happening on all platforms?
But you can count on this happening on all platforms even if it is not specified by the language standard.

There is no platform you might port your C code to in which that isn't the standard hardware behavior and no one implementing a compiler would go to the extra effort required to make that not work, even if they also wouldn't go to any extra effort to make it work.

Last edited by johnsfine; 03-24-2009 at 06:33 PM.
 
Old 03-24-2009, 06:44 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by khodeir View Post
if you want more : there are libraries for encryption that has 1000 bit as i think that will make all things large
If it is about big integers, try for the GMP library for integers as large as fit in memory.
 
Old 03-24-2009, 07:50 PM   #6
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
Quote:
is this guaranteed to happen by the C language?
Yes the standard guarantees that the addition of two unsigned integers will silently wrap (not overflow).

Quote:
6.5.2.9
The range of nonnegative values of a signed integer type is a subrange of the
corresponding unsigned integer type, and the representation of the same value in each
type is the same.31) A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value that can be
represented by the resulting type.
This guarantee is used in code for example in a hashing function.

Quote:
there also may be a question of which version of the language standard)
The above quote is from c99 9899 1999

Last edited by Biddle; 03-24-2009 at 08:01 PM. Reason: added the standard version
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned long int uint32_t to unsigned char and back MicahCarrick Programming 2 08-02-2009 01:39 AM
Adding Unsigned Integers of 128 bit bluechicken Programming 1 06-27-2005 02:27 PM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
64-bit integers in C nodger Programming 1 03-31-2005 08:06 AM
convert integers to ascii raven Programming 3 02-15-2002 03:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration