Declaring a Long using hexadecimal [duplicate] - java

This question already has answers here:
Initialize a long in Java
(4 answers)
Closed 8 years ago.
When declaring the number 0xFFFFFFFF (4294967295) as a long, java will set the variable to the number -1.
public static final long Bits32 = 0xFFFFFFFF; //Bits32 = -1
I assume that java initially converts the hexadecimal to an integer and then converts this to the long variable.
The obvious work around would to set Bits32 to the number 4294967295 instead. However this doesn't seem like a neat solution to me.
Dose anyone know how I would be able to declare a long to this number without having to manually convert the hexadecimal?
Cheers,
Chris.

Define as a Hex Long:
long Bits32 = 0xFFFFFFFFL; //Bits32 = -1
System.out.println(Bits32);
System.out.println(Long.toHexString(Bits32));
Note the L at the end of the 0xFFFFFFFF
The output is
4294967295
ffffffff

Related

want to assigning 12 digit long number in variable [duplicate]

This question already has answers here:
Initialize a long in Java
(4 answers)
Closed 2 years ago.
Want to assign 12 digits number in a long type variable
error: integer number too large: 100000000000
long no = 100000000000;
long no = 100000000000;
You need to treat the literal as a long as well using "L" at the end of the number:
long no = 100000000000L;
Without the literal, the number is treated as an int.
This is because by default every constant number you're assigning to a variable is seen as an int. To make your example work, you need to add an "L" at the end of the number.
long no = 10000...L;

Can I store a big hexadecimal value in Long datatype in Java? [duplicate]

This question already has answers here:
Convert from one base to another in Java
(10 answers)
Closed 5 years ago.
Mac address is 6 bytes long(12 digit or 48bits in length).
Long can hold 8 bytes of data but still I am not able to store following mac addresss in a long data variable?
Long mac=(long) 0xffffffffffff;
Why is it happening?
What is the best datatype to store mac address data type(I want to store in base10 format)?
You have to define it as Long value by adding a L at the end:
Long mac = 0xffffffffffffL;
You can find more information of the L in the Java SE Spec (3.10.1. Integer Literals).
You are not using a long value but an int. Just add L to your number
Long mac= 0xffffffffffffL;
To quote Oracle
An integer literal is of type long if it ends with the letter L or l;
otherwise it is of type int. It is recommended that you use the upper
case letter L because the lower case letter l is hard to distinguish
from the digit 1.
As for your question about which datatype to chose, any can be good as long as it holds the necessary 48bits.
Noteworthy, java returns a byte array in order to get the harware address in NetworkInterface#getHardwareAddress, but states that it is 'usually' a mac address.

A value over size of integer [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 5 years ago.
I'm a Vietnamese so my English's not good, please sympathize me. Thanks in advance.
I have a question.
I input a integer value but if it would been out of the size of int.
Ex: I input a = 1323544875154846543513521
So how to catch the error?
I must input in and then check the value?
Simple: don't use int, use Use BigInteger.
You might also wanna look other JVM languages that have number auto-casting, that is, automatically changing the number type according to the value at runtime (e.g. Clojure).
I input a integer value but if it would been out of the size of int.
Ex: I input a = 1323544875154846543513521 So how to catch the error?
System.out.println(Integer.MAX_VALUE+1); // anymore than the max will wrap around, value printed = -2147483648
System.out.println(Integer.MIN_VALUE-1); // if less than the min it will also wrap around, value printed = 2147483647
you could use Math.toIntExact(long value) if you want to receive an exception on overflow/underflow.
Alternatively, you could use BigInteger or BigDecimal which have no limit in size (your RAM is the limit).

Why I cannot parse "+1" to int? [duplicate]

This question already has answers here:
Parse number with positive/negative prefix from string in java
(2 answers)
Closed 8 years ago.
I am trying to parse +1 value to an int value. In my code +1 is a String and i am trying to convert it to Integer using Integer.parseInt,
String num = "+1";
int convertedNum = Integer.parseInt(num);
but I am getting error, why Java is not treating +1 as an integer value ?
because you must be using older version of JDK (JDK < 7). Before Java 7,
+1 wasn't considered as a valid integer.
Prior to JDK 1.7, what was the result of the following code segment?
double x = Double.parseDouble("+1.0");
int n = Integer.parseInt("+1");
Pat yourself on the back if you knew the
answer: +1.0 has always been a valid floating-point number, but
until Java 7, +1 was not a valid integer. This has now been fixed
for all the various methods that construct int, long, short, byte,
and BigInteger values from strings. There are more of them than you
may think. In addition to parse (Int|Long|Short|Byte), there are
decode methods that work with hexadecimal and octal inputs, and
valueOf methods that yield wrapper objects. The BigInteger(String)
constructor is also updated.

Why can't I assign a 'long' a value of 4 billion? [duplicate]

This question already has answers here:
The literal xyz of type int is out of range
(5 answers)
Closed 1 year ago.
I'm trying to declare a long value in Java, which unfortunately does not work.
This is my code. It results in the following error message: "The literal 4294967296 of type int is out of range".
long bytes = 4294967296;
I need this value to make a file filter that filters out files that are bigger than 4294967296 bytes (4GB). The other way round works without any issues (long size = file.length()) with every file size, which is why I can't figure out why my declaration is not working.
Add L to the end of the number:
long bytes = 4294967296L;
To answer your question title, the maximum value of a long can be obtained via the constant:
Long.MAX_VALUE
To solve your problem - add the l (L) literal after the number.
long literals are followed by the letter L or l (see: JLS 3.10.1). Uppercase is better because it's more legible, lowercase l looks too similar to 1.
For your particular number, it's probably easier to write:
long bytes = (1L << 32);
This way, someone who reads the code can quickly tell that bytes is exactly 2 to the power of 32.
try long bytes = 4294967296L; to indicate to the compiler that you are using a long.
The answer to your question "why" is because of 4294967296 is not a long. By default java look on any number as on int or double type (depending on if it has or hasn't dot). And only then convert this number to specified type (long in your case). So the error that you see means that your number is bigger then max value fot int. Adding literal attribute at the end let compiller know which type to use (b - bytes, s - short, l - long, f - float)
Soufiane is correct. Here is a doc that shows how to declare literals of the various types of numbers in Java:
http://www.janeg.ca/scjp/lang/literals.html

Categories