Java o Java int
sarà sempre un intero con segno a 32 bit?È un Java int sempre 32 bit?
risposta
Sì, è definito in The Java Language Specification.
Da Section 4.2: Primitive Types and Values:
I tipi integrali sono
byte
,short
,int
elong
, i cui valori sono 8 bit, 16 bit, 32 bit e 64 bit con segno two's- complemento numeri interi, rispettivamente, echar
, i cui valori sono numeri interi senza segno a 16 bit che rappresentano unità di codice UTF-16 (§3.1).
E inoltre da Section 4.2.1: Integral Types and Values:
I valori dei tipi integrali sono interi nei seguenti intervalli:
- Per byte, da -128 a 127, compreso
- In breve, da -32768 a 32767, compreso
- Per int, da -2147483648 a 2147483647, compreso
- Per molto tempo, da -9223372036854775808 a 9223372036854775807, comprensivo
- Per char, da '\ u0000' a '\ uffff' inclusiva, cioè, da 0 a 65535
int
s sono 32 bit. Se hai bisogno di più, gli long
s sono a 64 bit.
Java 8 ha aggiunto il supporto per gli interi non firmati. Il primitivo int
è ancora firmato, tuttavia alcuni metodi li interpreteranno come non firmati.
I seguenti metodi sono stati aggiunti al Integer class in Java 8:
- compareUnsigned (int x, int y)
- divideUnsigned (int dividendo, int divisore)
- parseUnsignedInt (String s)
- parseUnsignedInt (String s, int radix)
- remainderUnsigned (int dividendo, int divisore)
- toUnsignedLo ng (int x)
- toUnsignedString (int i)
- toUnsignedString (int i, int radix)
Ecco un esempio di utilizzo:
public static void main(String[] args) {
int uint = Integer.parseUnsignedInt("4294967295");
System.out.println(uint); // -1
System.out.println(Integer.toUnsignedString(uint)); // 4294967295
}
Come complementare, se lungo 64 bit non soddisfa i tuoi requisiti, prova java.math.BigInteger.
È adatto per la situazione in cui il numero è oltre l'intervallo di 64 bit.
public static void main(String args[]){
String max_long = "9223372036854775807";
String min_long = "-9223372036854775808";
BigInteger b1 = new BigInteger(max_long);
BigInteger b2 = new BigInteger(min_long);
BigInteger sum = b1.add(b1);
BigInteger difference = b2.subtract(b1);
BigInteger product = b1.multiply(b2);
BigInteger quotient = b1.divide(b1);
System.out.println("The sum is: " + sum);
System.out.println("The difference is: " + difference);
System.out.println("The product is: " + product);
System.out.println("The quotient is: " + quotient);
}
Il risultato è:
La somma è: 18446744073709551614
La differenza è: -18446744073709551615
Il prodotto è: -85070591730234615856620279821087277056
Il quoziente è: 1
- 1. È int in C Sempre a 32 bit?
- 2. Hash 32 bit int a 16 bit int?
- 3. Come convertire l'int firmato a 32 bit int a 32 bit senza segno int?
- 4. Compatibilità Java 32-bit vs 64-bit
- 5. Converti 12 bit int a 16 o 32 bit
- 6. È possibile eseguire l'Hack bit 5-Op Log2 (Int 32) in Java?
- 7. Perché le operazioni di spostamento dei risultati sempre in un int firmato quando operando è <32 bit
- 8. 32 bit è una breve
- 9. Java ed Eclipse - 32 vs 64 bit
- 10. Dimensioni di memoria di un sistema Java a 32 bit int [] array
- 11. È sempre 64 bit in entrambe le macchine a 32 e 64 bit
- 12. Int lungo lungo su macchine a 32 bit
- 13. Mappare un float a 32 bit su un intero a 32 bit
- 14. Vettore di doppio bit più veloce a 64 bit rispetto a un vettore di int non firmato a 32 bit?
- 15. Operatore bit a bit per ottenere byte da 32 bit
- 16. 64 bit per divisione 32 bit
- 17. typecasting implicito in C (conversione a 32 bit senza segno a 8 bit u INT)
- 18. Java: implementazione fp a 32 bit di Math.sqrt()
- 19. Quale bit è avanti per un intero in Java
- 20. Posso eseguire un programma java sviluppato usando jdk a 32 bit con jre a 64 bit? l'applicazione utilizza librerie di sistemi a 32 bit non java
- 21. iPhone OS 64 bit o 32 bit?
- 22. Determinazione dell'architettura JRE 32-bit vs 64-bit
- 23. Lettura a 16 bit da un registro a 32 bit
- 24. Java Native Interface dll a 32 bit su un sistema a 64 bit
- 25. C - Come verificare se 8 bit sono a 32 bit?
- 26. C++: è sicuro confrontare un intero a 64 bit con un intero a 32 bit?
- 27. Java 7 da eseguire su 32 bit su mac
- 28. sostituire byte a 32 bit numero
- 29. Int * array [32] è un puntatore a un array di 32 pollici o un array di 32 puntatori a int? Importa?
- 30. Come posso impacchettare un int come big endian a 32 bit in Perl?
Informazioni eccellenti :-) – joe