Ho questo codice in C (è per lo studio solo):C spostamento a sinistra di 64 bit fallire
char x;
uint64_t total = 0;
for(x = 20; x < 30; x++){
total = (((((1 << x) * x)/64) + 1) * sizeof(uint64_t));
printf("%d - %llu\n", x, total);
}
ciò che è stampato:
20 - 2621448
21 - 5505032
22 - 11534344
23 - 24117256
24 - 50331656
25 - 104857608
26 - 218103816
27 - 18446744073625665544
28 - 18446744073575333896
29 - 18446744073508225032
Perchè per x> 26 devo quelli strani valori? Sono a gcc 4.6.1 su Ubuntu 10.10 64 bit.
sembra che il problema sia in char x, ho usato uint64_t per questo e funziona bene. –
Sarai in grado di vedere cosa sta accadendo più chiaramente se 'total = (((((1 << x) * x)/64) + 1) * sizeof (uint64_t));' è semplificato in 'total = ((1 << x) * x); 'stampato con' printf ("% d - 0x% llx \ n", x, totale); 'vedrai il bit del segno che gira il negativo nmber, che viene stampato come un numero enorme per printf ("... -% llu \ n", ... totale); – gbulmer