Created
March 15, 2021 11:10
-
-
Save JUNNETWORKS/32bfbc489b8ea0b5d1c1c5d35ccd15f5 to your computer and use it in GitHub Desktop.
visualize little endian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#include "unistd.h" | |
int main(){ | |
char *s = "1234"; | |
printf("%p\n", s); | |
for (int i = 0; i < 4; i++) | |
printf("s[%d]: %c\n", i, *(s+i)); | |
write(STDOUT_FILENO, s, 2); | |
printf("\n"); | |
// Ascii的にABCDだけどリトルディアンなので, 自分の環境はリトルエンディアンメモリ上の並びはDCBAになる | |
int num = 0x41424344; | |
// ビッグエンディアン: 大きい桁が先頭アドレスに格納 | |
// リトルエンディアン: 小さい桁が先頭アドレスに格納 | |
for (int i = 0; i < 4; i++) | |
printf("num[%d]: %c\n", i, *((char*)(&num)+i)); | |
write(STDOUT_FILENO, &num, 4); | |
printf("\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment