prev up next


Previous: Einfache Datentypen Up: Einfache Datentypen Next: Gleitkommazahlen (float, double)

Subsections


Ganze Zahlen (byte, short, int, long)

Wertebereich: ganze Zahlen darstellbar in 8, 16, 32, 64 Bits.
Typ Wertebereich Länge
byte -128..127 8 Bit
short -32768..32767 16 Bit
int -2147483648..2147483647 32 Bit
long -9223372036854775808..9223372036854775807 64 Bit

Codierung

Codierung der positiven Zahlen in Dualzahldarstellung:

Sei

x = di · 2i

Algorithmus dezimal dual:

while (x != 0){
   if (x%2 == 0) IO.print('0');
            else IO.print('1');
   x = x/2;
}
Obacht: Bits werden rückwärts generiert!

Codierung der ganzen Zahlen im 2-er Komplement:

d3 d2 d1 d0 x
0 1 1 1 7
0 1 1 0 6
0 1 0 1 5
0 1 0 0 4
0 0 1 1 3
0 0 1 0 2
0 0 0 1 1
0 0 0 0 0
1 1 1 1 -1
1 1 1 0 -2
1 1 0 1 -3
1 1 0 0 -4
1 0 1 1 -5
1 0 1 0 -6
1 0 0 1 -7
1 0 0 0 -8

Beispiel zur Berechnung des 2-er Komplements einer negativen Zahl:

Gegeben - x -4
Finde di zu x 0100
Invertiere Bits 1011
Addiere 1 1100

Vorteil: Nur ein Addierwerk!

0011 3
+ 1011 -5
= 1110 -2

Subtraktion mittels Negierung auf Addition zurückführen. Obacht: Überlauf beachten!

0111 7  
+ 0001 1  
= 1000 -8 falsch

Trick: Vorzeichenbits verdoppeln, müssen nach der Verknüpfung identisch sein:

00111 7 00011 3        
+ 00001 1 11011 -5        
01000   11110 -2        
Ergebnis undefiniert   Ergebnis ok!          

Operatoren

+ : Ganzzahl × Ganzzahl Ganzzahl Addition
- : Ganzzahl × Ganzzahl Ganzzahl Subtraktion
* : Ganzzahl × Ganzzahl Ganzzahl Multiplikation
/ : Ganzzahl × Ganzzahl Ganzzahl ganzzahlige Division
% : Ganzzahl × Ganzzahl Ganzzahl Modulo = Rest der Division
& : Ganzzahl × Ganzzahl Ganzzahl bitweise Und-Verknüpfung
| : Ganzzahl × Ganzzahl Ganzzahl bitweise Oder-Verknüpfung
^ : Ganzzahl × Ganzzahl Ganzzahl bitweise XOR-Verknüpfung
<< : Ganzzahl × Ganzzahl Ganzzahl Linksshift
>> : Ganzzahl × Ganzzahl Ganzzahl Vorzeichen erhaltender Rechtsshift
>>> : Ganzzahl × Ganzzahl Ganzzahl Vorzeichen ignorierender Rechtsshift
~ : Ganzzahl Ganzzahl Bitweise Negation
- : Ganzzahl Ganzzahl Vorzeichenumkehr
++ : Ganzzahl Ganzzahl Inkrement
-- : Ganzzahl Ganzzahl Dekrement

Konstantenbezeichner

123 +123 -123

Eine führende Null kündigt eine Oktalzahl zur Basis 8 an: 0173. Eine führende Null mit nachfolgendem x oder X kündigt eine Hexadezimalzahl zur Basis 16 an: 0x7B.


prev up next
Previous: Einfache Datentypen Up: Einfache Datentypen Next: Gleitkommazahlen (float, double)