initializing an int variable with all F's

initializing an int variable with all F's

Post by Pietro Cer » Thu, 30 Aug 2007 08:20:49



Could you name an integer type whose width (in bits) sn't a multiple of 4?



--
Pietro Cerutti

PGP Public Key:
http://www.yqcomputer.com/
 
 
 

initializing an int variable with all F's

Post by junky_fell » Thu, 30 Aug 2007 15:10:15

Guys,

I was just looking at some code where to initialize an integer
with all F's following statement is used;

unsigned int i = -1;

I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?

 
 
 

initializing an int variable with all F's

Post by Ark Khasi » Thu, 30 Aug 2007 15:20:48


unsigned int i = ~0U;
initializes to all 1's. There is no way to initialize to all
(presumably, hex) F's on a hypothetical 41-bit machine.
 
 
 

initializing an int variable with all F's

Post by Malcolm Mc » Thu, 30 Aug 2007 17:48:42


It is one of those awkward things.
In practise you are never likely to program a non-two's complement machine.
However one's complement and sign magnitude are allowed (one place you will
see non-two's complement integers is in the exponent of an IEEE floating
point number).
So i = ~0; is actually the best way of achieving things. But you will see i
= -1 even in production code.

--
Free games and programming goodies.
http://www.yqcomputer.com/ ~bgy1mm
 
 
 

initializing an int variable with all F's

Post by CBFalcone » Thu, 30 Aug 2007 18:05:28


It's not an integer, it's an unsigned int. Not the same. And yes,
the unsigned int will be initialized to 2**n - 1. If "sizeof
unsigned int * CHAR_BIT" is a multiple of 8, that will be a
collection of hex f.

Copying that unsigned int into an int will normally cause undefined
behaviour, because it out of range for an int. That behaviour may
be exactly what you want, or anything else. You just don't know.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
< http://www.yqcomputer.com/ >



--
Posted via a free Usenet account from http://www.yqcomputer.com/
 
 
 

initializing an int variable with all F's

Post by ravi » Thu, 30 Aug 2007 19:09:04

On Aug 29, 11:10 am, " XXXX@XXXXX.COM "


Another way is :-

int i = 0xFFFF;
 
 
 

initializing an int variable with all F's

Post by Pietro Cer » Thu, 30 Aug 2007 19:17:00


Assuming 4-bytes int.


--
Pietro Cerutti

PGP Public Key:
http://www.yqcomputer.com/
 
 
 

initializing an int variable with all F's

Post by Pietro Cer » Thu, 30 Aug 2007 19:25:57


hem... assuming 2-bytes ints.

Check out this:

#include <stdio.h>
int main(void) {
unsigned int i = 0xFFFFU;
unsigned int ii = ~0U;
printf("i is %u, ll is %u\n", i, ii);
return (0);
}




--
Pietro Cerutti

PGP Public Key:
http://www.yqcomputer.com/
 
 
 

initializing an int variable with all F's

Post by pete » Thu, 30 Aug 2007 19:54:18


That's wrong.

unsigned int i = -1;
is guaranteed to put a value of UINT_MAX into object i.

i = ~0; operates on the sign bit of an int type value
and yields an implementation defined result.

N869
6.2.5 Types
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.

--
pete
 
 
 

initializing an int variable with all F's

Post by pete » Thu, 30 Aug 2007 19:55:17


So does unsigned int i = -1;

--
pete
 
 
 

initializing an int variable with all F's

Post by pete » Thu, 30 Aug 2007 19:57:38


That is one correct way to initialise an unsigned int
object with a value of UINT_MAX

--
pete
 
 
 

initializing an int variable with all F's

Post by Philip Pot » Thu, 30 Aug 2007 20:14:12


>>

One's complement and sign-magnitude are only allowed for /signed/ integers. The
OP clearly was asking about unsigned.

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
 
 

initializing an int variable with all F's

Post by pemo » Thu, 30 Aug 2007 21:29:34


<snip>

So, include limits.h and use

unsigned int i = UINT_MAX;

Can't see any real difference - except that the latter is *perhaps*
more self-documenting?

pemo
 
 
 

initializing an int variable with all F's

Post by Philip Pot » Thu, 30 Aug 2007 21:37:55


Why do you want to set a variable to all F's? I can't think of many reasons I'd
want to store UINT_MAX in a variable. (Curiosity is the only reason I can think
of offhand.)

Phil

--
Philip Potter pgp <at> doc.ic.ac.uk
 
 
 

initializing an int variable with all F's

Post by Kenneth Br » Fri, 31 Aug 2007 00:47:48


What if you're not on a two's complement system? Perhaps "-1" is
not represented by all-bits-one?

(OTOH, is "0U" guaranteed to be represented as all-bits-zero, and
therefore "~0U" all-bits-one?)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto: XXXX@XXXXX.COM >