Caner
Caner

Reputation: 59178

How to create a long value with all bits = 1

I tried the following:

public static final long DEVICE_ID_UNKNOWN = 0xFFFFFFFFFFFFFFFF;

but it causes

The literal 0xFFFFFFFFFFFFFFFF of type int is out of range

Upvotes: 4

Views: 5125

Answers (8)

Jomoos
Jomoos

Reputation: 13083

You may use either of the following:

public static final long DEVICE_ID_UNKNOWN = 0xFFFFFFFFFFFFFFFFL;

or

public static final long DEVICE_ID_UNKNOWN = ~0L;

or

public static final long DEVICE_ID_UNKNOWN = -1L;

Upvotes: 7

Peter Lawrey
Peter Lawrey

Reputation: 533530

Yet another way to get all 1s

public static final long DEVICE_ID_UNKNOWN = ~0L;

Upvotes: 3

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

You need to specify the type of the number if it can not be represented as an int. A long is a L or l (lowercase). I prefer uppercase as lowercase is easy to mistake for 1 (one).

public static final long DEVICE_ID_UNKNOWN = 0xFFFFFFFFFFFFFFFFL;

Or, you can just set the value to -1, but that might not be as clear in communicating the meaning "all bits 1".

Upvotes: 7

Kiril Kirilov
Kiril Kirilov

Reputation: 11257

public static final long DEVICE_ID_UNKNOWN = 0xFFFF_FFFF_FFFF_FFFFL;

Usefull reference http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Upvotes: 1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143099

Sorry, I'm not the java guy, but wouldn't -1 do?

Upvotes: 1

Graham Borland
Graham Borland

Reputation: 60681

public static final long DEVICE_ID_UNKNOWN = -1L;

Upvotes: 2

A.H.
A.H.

Reputation: 66263

Use 0xFFFFFFFFFFFFFFFFl and be fine. Another way would be to use simply -1 because this value also has all bits set. See http://en.wikipedia.org/wiki/Two%27s_complement for details.

Upvotes: 8

Anthony Grist
Anthony Grist

Reputation: 38345

Use public static final long DEVICE_ID_UNKNOWN = 0xFFFFFFFFFFFFFFFFL; instead to signify that it's a long value, not an int value.

Upvotes: 1

Related Questions