Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > signed/unsigned int

Reply
Thread Tools

signed/unsigned int

 
 
Aire
Guest
Posts: n/a
 
      01-22-2004
1. If a function defined as:

void test_function(unsigned int a)
{
}

Is "test_function(-256);" going to cause undefined behavior?

2. What's "a negative signed value wrapping"?

3. Is bit-shifting on a signed int undefined behavior? Why?

Thanks!


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-22-2004
Aire wrote:
>
> 1. If a function defined as:
>
> void test_function(unsigned int a)
> {
> }
>
> Is "test_function(-256);" going to cause undefined behavior?


No.

> 2. What's "a negative signed value wrapping"?


An informal description of one common consequence
of the result of trying to compute or store a value
smaller than the smallest value representable in the
given signed integer type.

> 3. Is bit-shifting on a signed int undefined behavior? Why?


Yes under some circumstances; because the Standard
doesn't define it.


--

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-23-2004
On Thu, 22 Jan 2004 17:36:02 GMT, "Aire" <> wrote in
comp.lang.c:

> 1. If a function defined as:
>
> void test_function(unsigned int a)
> {
> }
>
> Is "test_function(-256);" going to cause undefined behavior?


No, but it might cause unexpected errors in the function if such a
value is not valid and the function does not check for it. The value
-256 will be converted, as if by assignment, to an unsigned int to
pass to the function.

Assigning a negative value to a unsigned integer type can never
overflow and the result is always well-defined. In this case, the
unsigned int value passed will be UINT_MAX + 1 - 256. That's 65280
for typical 16 bit ints, or 4294967040 for typical 32 bit ints, but
other values are possible.

> 2. What's "a negative signed value wrapping"?


No such thing in C. Overflowing or underflowing a signed integer type
produces undefined behavior.

> 3. Is bit-shifting on a signed int undefined behavior? Why?


It is possible that some bit patterns, when interpreted as a signed
integer type, do not compose a valid value for that type. This is
called a trap representation, and dealing with such produces undefined
behavior. When shifting a signed integer, it is possible that a trap
representation may be produced.

Aside from that, it is implementation-defined whether right shifting a
signed integer type preserves the sign bit.

> Thanks!


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57