Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Setting "all ones"

Reply
Thread Tools

Setting "all ones"

 
 
John Devereux
Guest
Posts: n/a
 
      05-19-2005

I was wondering what is the "best" way to set a variable to "all
ones". In particular where I do not know the precise type of the
variable (or more accurately, when I don't want to worry about it!).

E.g. assume "a" is unsigned, but could be char, short, int or long.

a = -1;

or

a = ~0;

Would these always work? (I only care about machines with 2's
complement arithmetic).

Thanks,

--

John Devereux
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      05-19-2005
John Devereux wrote:
>
> I was wondering what is the "best" way to set a variable to "all
> ones". In particular where I do not know the precise type of the
> variable (or more accurately, when I don't want to worry about it!).
>
> E.g. assume "a" is unsigned, but could be char, short, int or long.
>
> a = -1;


Yes, always

>
> or
>
> a = ~0;


Should be ~0u, instead.
The type of ~0 is int,
and the value of ~0 depends on binary representation.
In sign and magnitude, ~0 is -INT_MAX.
In one's complement ~0 is negative zero, equal to zero.
Assignement is by value.

> Would these always work? (I only care about machines with 2's
> complement arithmetic).


Since it's easy to avoid two's complement issues, you should.

--
pete
 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      05-19-2005
"pete" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Devereux wrote:
> > I was wondering what is the "best" way to set a variable to "all
> > ones". In particular where I do not know the precise type of the
> > variable (or more accurately, when I don't want to worry about it!).
> >
> > E.g. assume "a" is unsigned, but could be char, short, int or long.
> >
> > a = -1;

>
> Yes, always
>
> > or
> >
> > a = ~0;

>
> Should be ~0u, instead.


But that will only set up to as many bits as there are in an (unsigned) int.
~0 will always work for two's complement because it produces the value -1
(of type int), making it equivalent to the first method. However, as you
pointed out, it produces different values for other signed integer
representations.

Alex


 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      05-19-2005
In article <(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> John Devereux wrote:
> > I was wondering what is the "best" way to set a variable to "all
> > ones". In particular where I do not know the precise type of the
> > variable (or more accurately, when I don't want to worry about it!).

....
> > E.g. assume "a" is unsigned, but could be char, short, int or long.
> > a = -1;

>
> Yes, always
>
> > a = ~0;

>
> Should be ~0u, instead.
> The type of ~0 is int,
> and the value of ~0 depends on binary representation.
> In sign and magnitude, ~0 is -INT_MAX.
> In one's complement ~0 is negative zero, equal to zero.
> Assignement is by value.


Yup, the value is not -1, but all bits are set to 1. This contradicts
your first "yes always".
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-19-2005
Alex Fraser wrote:
>
> "pete" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > John Devereux wrote:
> > > I was wondering what is the "best" way to set a variable to "all
> > > ones". In particular where I do not know the precise type of the
> > > variable
> > > (or more accurately, when I don't want to worry about it!).
> > >
> > > E.g. assume "a" is unsigned,
> > > but could be char, short, int or long.
> > >
> > > a = -1;

> >
> > Yes, always
> >
> > > or
> > >
> > > a = ~0;

> >
> > Should be ~0u, instead.

>
> But that will only set up to as many bits
> as there are in an (unsigned) int.
> ~0 will always work for two's complement because
> it produces the value -1 (of type int),
> making it equivalent to the first method. However, as you
> pointed out, it produces different values for other signed integer
> representations.


I neglected to read this part:

> > > but could be char, short, int or long.


--
pete
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-19-2005
John Devereux wrote:
> I was wondering what is the "best" way to set a variable to "all
> ones". In particular where I do not know the precise type of the
> variable (or more accurately, when I don't want to worry about it!).
>
> E.g. assume "a" is unsigned, but could be char, short, int or long.
>
> a = -1;
>
> or
>
> a = ~0;
>
> Would these always work? (I only care about machines with 2's
> complement arithmetic).


For any unsigned type on any system (even those that are not 2s complement)
a = -1;
This is because of the way conversion to unsigned types is defined in C.

I think that a = ~1; will work on a 2s complement machine because it
takes a signed int 0, inverts all bits giving you a signed int -1, then
the rules for assigning -1 to an unsigned int make it work.

So in this instance there is no real cost to making it fully portable.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
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
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 2 08-22-2006 09:30 PM
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 0 08-21-2006 11:55 PM
python-dev and setting up setting up f2py on Windows XP Sile Python 5 08-18-2006 08:13 AM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM
Setting a configurable time-out setting for all the communication initiated with Remoting object. Srinivasa Raghavan Sethuraman ASP .Net 0 06-30-2004 10:05 AM



Advertisments