2
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?

More than 3 years have passed since last update.

bit と byteについて

Last updated at Posted at 2020-06-22

bit

binary digit の略。
二進の数字、つまり二進数。

2進法の1桁分の情報のことをbitと呼ぶ。

2進法は、1桁で0または1という2通りの情報を表現できる。
2ビットであれば4情報(2の2乗)
3ビットであれば8情報(2の3乗)を表現することができる。

コンピュータにおいて、最も基本的な単位。

byte

bite(噛む)をもじった造語。
情報を "ひと噛み" して、その塊を情報の基本単位としよう的な意味。

8bitで1byte。
1byteで256通り(2の8乗)の数字を表すことができる。
256=16×16なので、16進数と相性がいい。

byteを2進数で表現すると長くなるから、2進数8桁を16進数2桁に短くした表現が使われる。

バイトを可視化するコード

# include <stdio.h>

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, size_t len)
{
	size_t i;
	for (i = 0; i < len; i++)
		printf(" %.2x", start[i]);
	printf("\n");
}

void show_int(int x)
{
	show_bytes((byte_pointer) &x, sizeof(int));
}

void show_float(float x)
{
	show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x)
{
	show_bytes((byte_pointer) &x, sizeof(double));
}

void test_show_bytes(int val)
{
	int ival = val;
	float fval = (float)ival;
	void *pval = &ival;

	printf("%20d = ", ival);
	show_int(ival);
	printf("%20f = ", fval);
	show_float(fval);
	printf("%20p = ", pval);
	show_pointer(pval);
}

int main()
{
	// 0x00003039
	test_show_bytes(12345);
	return (0);
}

参考

bit と byte の語源 - Qiita

ビットとバイトの違いは?Webに携わるなら知っておきたい7つの単位を解説|ferret

おすすめ
プログラムとバイナリ - Qiita

2
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
2
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?