Setting "all ones"

J

John Devereux

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,
 
P

pete

John said:
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.
 
A

Alex Fraser

pete said:
Yes, always


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
 
D

Dik T. Winter

>
> Yes, always
>
>
> 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".
 
P

pete

Alex said:
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:
 
F

Flash Gordon

John said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top