0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

バイトオーダーについて #C

Last updated at Posted at 2025-09-23

今回はバイトオーダーに関する学習を行います。

バイトオーダとは

バイトオーダ(byte order)とは複数のバイト(2バイト以上)からなるデータがあったときにそのバイトを並び替える順番を指す

テストコード

#include <stdio.h>
/*バイトオーダのテストコード*/
int main(void){
    int     hoge = 0x12345678;
    unsigned char   *hoge_p = (unsigned char*)&hoge;

    printf ("%x\n",hoge_p[0]);
    printf ("%x\n",hoge_p[1]);
    printf ("%x\n",hoge_p[2]);
    printf ("%x\n",hoge_p[3]);
    return 0;
}

結果

78
56
34
12

ポイント

ポイントとなるのは以下の3点

  • 私の環境での結果はhogeの値を逆向き(後ろから)にメモリへと格納している
  • この格納方法はCPUによって異なる
  • 今回の実験のような格納方法を「リトルエンディアン」と呼ぶ

解説

  • 私の環境での結果はhogeの値を逆向き(後ろから)にメモリへと格納している
  • この格納方法はCPUによって異なる

今回私はAMD製のCPUを使用している。AMDやintel製のCPUでは後述のリトルエンディアンが使われることが多い

  • 今回の実験のような格納方法を「リトルエンディアン」と呼ぶ

リトルエンディアンと逆順でアドレスを格納することを指す
反対に先頭から格納することを「ビッグエンディアン」と呼ぶ

まとめ

色々調べてたが、記事にも書いたとおり環境依存の機能であることがわかった。
今後の開発の際、CPUが違った場合の処理を記述する時などに、考慮すべき場面も出てくると思われる。ただ、本格的な開発を行っていない段階ではあまり参考にする場面が少ないと考えられる。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?