Endianess und typecast

Gast #4144786
Lesenswert?

Hallo,

ich verwirre mich gerade mit big/little endianess und typecasts.

Ich beziehe mich auf diese Darstellung
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/I1835.html

Wenn ich eine little-endian-Maschine habe, steht LSB bis LSB+7 an 
Adresse 0. Bei einer big-endian-Maschine steht MSB-7 bis MSB and Adresse 
0 (Bitorder innerhalb der Bytes mal unbeachtet).

Mache ich folgendes
1
uint32_t a  = 0x01020304;
2
uint8_t b   = (uint8_t)a;
3
uint8_t * c = &a;

ist b dann sowohl bei einer little- als auch big-endian-Maschine 0x04? 
D.h. bei einer little-endian-Maschine nimmt der Compiler das Byte an 
Adresse 0 und bei einer big-endian-Maschine das Byte an Adresse 3 für 
diesen Cast?
Für *c bekomme ich wahrscheinlich 0x04 auf einer little-endian- und 0x01 
auf einer big-endian-Maschine?
Moderator Persönliche Seite #4144793
Lesenswert?

Hast Du keinen C-Compiler?

$ cat a.c
1
#include <stdio.h>
2
#include <stdint.h>
3

4
int main ()
5
{
6
    uint32_t a  = 0x01020304;
7
    uint8_t b   = (uint8_t)a;
8
    uint8_t * c = (uint8_t *) &a;   // cast hinzugefuegt
9

10
    printf ("%d %d %d\n", a, b, *c);
11
    return 0;
12
}
$ cc -Wall a.c && ./a.out
16909060 4 4

Übersetzt auf einer Little-Endian-Maschine (Linux).

P.S.
Deine Überlegungen sind alle richtig. b ist auf einer 
Big-Endian-Maschine identisch, *c jedoch nicht.
#4144798
Lesenswert?

Endianer schrieb:
> ist b dann sowohl bei einer little- als auch big-endian-Maschine 0x04?
> D.h. bei einer little-endian-Maschine nimmt der Compiler das Byte an
> Adresse 0 und bei einer big-endian-Maschine das Byte an Adresse 3 für
> diesen Cast?

ja

> Für *c bekomme ich wahrscheinlich 0x04 auf einer little-endian- und 0x01
> auf einer big-endian-Maschine?

ja (ggf. noch ein cast nötig)

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren