4

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
Caner
  • 57,267
  • 35
  • 174
  • 180
  • 1
    possible duplicate of [Initialize a long in Java](http://stackoverflow.com/questions/6834037/initialize-a-long-in-java) – MByD Dec 20 '11 at 10:09

8 Answers8

8

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.

A.H.
  • 63,967
  • 15
  • 92
  • 126
7

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".

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
6

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;
Jomoos
  • 12,823
  • 10
  • 55
  • 92
3

Yet another way to get all 1s

public static final long DEVICE_ID_UNKNOWN = ~0L;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • `~0` means make every bit a 1 (the opposite of 0) whereas -1 happens to be all 1s as a `long` (but not as a `double`) – Peter Lawrey Dec 20 '11 at 11:06
  • Can you explain? I didn't understand what you mean by 'all 1s as a long (but not as a double)'. – user1071840 May 28 '14 at 14:58
  • a double has a different bit format than a long - see IEEE 754: http://en.wikipedia.org/wiki/Double-precision_floating-point_format – vazor Feb 18 '15 at 16:51
  • 1
    I believe this is the easiest alternative to understand. And you don't need to triple-check the number of Fs either. – matiash Nov 11 '16 at 18:48
2
public static final long DEVICE_ID_UNKNOWN = -1L;
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
1

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

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
1

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

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1
public static final long DEVICE_ID_UNKNOWN = 0xFFFF_FFFF_FFFF_FFFFL;

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

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74