Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > initializing an int variable with all F's

Reply
Thread Tools

initializing an int variable with all F's

 
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-28-2007
Keith Thompson wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
>> 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 ?

>
> Can you be more specific about your requirement?
>
> You wrote "int" in your subject header, "integer" in the body of your
> article, and "unsigned int" in your example.
>
> "all F's" is a very strange requirement, or at least a very strange
> way to express it. I assume you mean "0xF...F", where the number of
> Fs depends on the width of the type. What if the width of the type
> (in bits) isn't a multiple of 4?


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

>
> Do you want the maximum value of some type? If so, which one?
>



--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
 
 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      08-29-2007
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 ?

 
Reply With Quote
 
 
 
 
Ark Khasin
Guest
Posts: n/a
 
      08-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 ?
>

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.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      08-29-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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 ?
>

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.personal.leeds.ac.uk/~bgy1mm


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-29-2007
"(E-Mail Removed)" wrote:
>
> 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 ?


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://cbfalconer.home.att.net>



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

 
Reply With Quote
 
ravi
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 11:10 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> 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 ?


Another way is :-

int i = 0xFFFF;

 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-29-2007
ravi wrote:
> On Aug 29, 11:10 am, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
>> 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 ?

>
> Another way is :-
>
> int i = 0xFFFF;


Assuming 4-bytes int.


--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      08-29-2007
Pietro Cerutti wrote:
> ravi wrote:
>> On Aug 29, 11:10 am, "(E-Mail Removed)"
>> <(E-Mail Removed)> wrote:
>>> 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 ?

>> Another way is :-
>>
>> int i = 0xFFFF;

>
> Assuming 4-bytes int.


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://gahr.ch/pgp
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-29-2007
Malcolm McLean wrote:
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > 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;


> So i = ~0; is actually the best way of achieving things.


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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-29-2007
Ark Khasin wrote:
>
> (E-Mail Removed) wrote:
> > 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 ?
> >

> unsigned int i = ~0U;
> initializes to all 1's.


So does unsigned int i = -1;

--
pete
 
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