|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
[Java] int byte bit shift...@#?!!
Vorrei cambiare il valore del quarto byte di un int;
la domanda è, devo per forza azzerare il valore precedente degli ultimi 8 bit e poi reimpostarli o c'è un modo per sostituirli che non richieda un doppio shift sinistro-destro di 8 bit? Per intenderci: c=((c<<8)>>8)+(x+1<<24); dove "c" è l'int da "operare" e x un altro int da cui prendo gli 8 bit per la sostituzione. E' inguardabile! Grazie &Ciao. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Puoi azzerare il byte da sostituire con una AND...
c = (c & 0xffffff) || (x << 24); |
|
|
|
|
|
#3 |
|
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Mi sento un po' stupido, ho usato l'AND nelle 5 righe sopra e alla sesta ho dimenticato che esistesse...
Grazie Cionci, sei il mio salvatore! &Ciao. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Figurati...no problem...
Comunque quel codice presuppone la diemensione degli int... Si può cercare di farci qualche lavoretto sopra per non tenerne conto... c = (c & (~(0xff << (sizeof(int)-1)*8)) || (x << (sizeof(int)-1)*8)); |
|
|
|
|
|
#5 |
|
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Fortunatamente in Java gli int hanno dimensione fissa (32bit).
Il sizeOf invece è una cosa che invidio da morire (sopratutto per gli oggetti, se non sbaglio c'è una versione anche per quelli forse in c++), scoprire quanta memoria si consuma è un terno al lotto |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Sì...funziona anche sugli oggetti...
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
tratto da Javaworld ti posto questo empirico metodo per calcolare la dimensione di oggetti di una tua classe.
basta che sostituisci a Object la classe che ti interessa. ripeto: empirico, serve solo per farsi un idea Codice:
public class Sizeof
{
public static void main (String [] args) throws Exception
{
// Warm up all classes/methods we will use
runGC ();
usedMemory ();
// Array to keep strong references to allocated objects
final int count = 100000;
Object [] objects = new Object [count];
long heap1 = 0;
// Allocate count+1 objects, discard the first one
for (int i = -1; i < count; ++ i)
{
Object object = null;
// Instantiate your data here and assign it to object
object = new Object ();
//object = new Integer (i);
//object = new Long (i);
//object = new String ();
//object = new byte [128][1]
if (i >= 0)
objects [i] = object;
else
{
object = null; // Discard the warm up object
runGC ();
heap1 = usedMemory (); // Take a before heap snapshot
}
}
runGC ();
long heap2 = usedMemory (); // Take an after heap snapshot:
final int size = Math.round (((float)(heap2 - heap1))/count);
System.out.println ("'before' heap: " + heap1 +
", 'after' heap: " + heap2);
System.out.println ("heap delta: " + (heap2 - heap1) +
", {" + objects [0].getClass () + "} size = " + size + " bytes");
for (int i = 0; i < count; ++ i) objects [i] = null;
objects = null;
}
private static void runGC () throws Exception
{
// It helps to call Runtime.gc()
// using several method calls:
for (int r = 0; r < 4; ++ r) _runGC ();
}
private static void _runGC () throws Exception
{
long usedMem1 = usedMemory (), usedMem2 = Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i)
{
s_runtime.runFinalization ();
s_runtime.gc ();
Thread.currentThread ().yield ();
usedMem2 = usedMem1;
usedMem1 = usedMemory ();
}
}
private static long usedMemory ()
{
return s_runtime.totalMemory () - s_runtime.freeMemory ();
}
private static final Runtime s_runtime = Runtime.getRuntime ();
} // End of class
|
|
|
|
|
|
#8 |
|
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Ero a conoscenza della "media" fatta con un ciclo simile (anzi, forse era proprio quello
Cmq grazie per la segnalazione, la gestione della memoria della jvm è un argomento piuttosto interessante, fa sempre bene vedere qualche riga che se ne occupa &Ciao. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:15.



















