如何生成一个随机的 32 位二进制数并使用 C 操作它/从中提取某些位?
How do I generate a random 32 bit binary number and manipulate it/ extract certain bits from it using C?
我了解到在C语言中,要生成一个随机数,我们使用rand();
我的计划是 (i) 生成一个二进制的 32 位随机数,
(ii) 出于特定目的从中提取一些特定位。
我试过使用 int rando = rand() 但这至少显示了十进制表示。这对我来说很好,但我需要从中提取某些位。我试过类似的东西:
unsigned long init32;
init32 = ((double)rand()/RAND_MAX)*0b11111111111111111111111111111111111111; printf(" The number is %lu", init32);
打印出来时没有给我二进制表示。
就像我说的,我需要提取一些特定的位。例如,
我应该如何为此目的生成 32 位二进制数,然后为页面 Table 存储 10 位?
希望我说得够清楚。这是一个研究项目。
“二进制”和“十进制”只是数字的书写方式。所以二十在十进制中写为 20,在二进制中写为 10100(在十六进制中写为 14),但在所有情况下它仍然是数字二十。
您的 printf
行是:
printf(" The number is %lu", init32);
当您写 %lu
时,因为它以 u
结尾,您实际上要求将值打印为(正)十进制数。虽然使用 printf
不能直接打印二进制值,但可以将其打印为十六进制,这完全等价:
printf(" The number is %lx", init32); // For example: " The number is 14", which means the number is "10100" in binary
从十六进制中很容易找到相同数字的二进制,因为每个十六进制字符直接对应一个二进制表示(例如“A”在二进制中是“1010”):https://www.bbc.co.uk/bitesize/guides/zp73wmn/revision/1 .
如果通过“提取特定位”,你的意思是获得与你的示意图中的位相对应的数字,你可以用这样的方法来做到这一点(我没有测试过,但这应该很好或非常接近) :
init32 = /* some value */;
// Basically the part on the left side of the "&" takes the entire init32
// and moves it right by that number of bits. Then to cancel the bits on the
// left that you don't want (such as to put to 0 the bits of the "directory"
// when you want to get the page table), we use bitwise "&" with the part on
// the right.
unsigned long directory = (init32 >> 22) & ((1 << (31 - 22 + 1)) - 1);
unsigned long pagetable = (init32 >> 12) & ((1 << (21 - 12 + 1)) - 1);
unsigned long offset = (init32 >> 0 ) & ((1 << (11 - 0 + 1)) - 1);
如果这让您感到困惑,请查看 Google 上的“C 按位运算符”。你确实需要了解数字如何以二进制形式工作才能了解它的作用。
我了解到在C语言中,要生成一个随机数,我们使用rand(); 我的计划是 (i) 生成一个二进制的 32 位随机数, (ii) 出于特定目的从中提取一些特定位。
我试过使用 int rando = rand() 但这至少显示了十进制表示。这对我来说很好,但我需要从中提取某些位。我试过类似的东西:
unsigned long init32;
init32 = ((double)rand()/RAND_MAX)*0b11111111111111111111111111111111111111; printf(" The number is %lu", init32);
打印出来时没有给我二进制表示。
就像我说的,我需要提取一些特定的位。例如,
我应该如何为此目的生成 32 位二进制数,然后为页面 Table 存储 10 位?
希望我说得够清楚。这是一个研究项目。
“二进制”和“十进制”只是数字的书写方式。所以二十在十进制中写为 20,在二进制中写为 10100(在十六进制中写为 14),但在所有情况下它仍然是数字二十。
您的 printf
行是:
printf(" The number is %lu", init32);
当您写 %lu
时,因为它以 u
结尾,您实际上要求将值打印为(正)十进制数。虽然使用 printf
不能直接打印二进制值,但可以将其打印为十六进制,这完全等价:
printf(" The number is %lx", init32); // For example: " The number is 14", which means the number is "10100" in binary
从十六进制中很容易找到相同数字的二进制,因为每个十六进制字符直接对应一个二进制表示(例如“A”在二进制中是“1010”):https://www.bbc.co.uk/bitesize/guides/zp73wmn/revision/1 .
如果通过“提取特定位”,你的意思是获得与你的示意图中的位相对应的数字,你可以用这样的方法来做到这一点(我没有测试过,但这应该很好或非常接近) :
init32 = /* some value */;
// Basically the part on the left side of the "&" takes the entire init32
// and moves it right by that number of bits. Then to cancel the bits on the
// left that you don't want (such as to put to 0 the bits of the "directory"
// when you want to get the page table), we use bitwise "&" with the part on
// the right.
unsigned long directory = (init32 >> 22) & ((1 << (31 - 22 + 1)) - 1);
unsigned long pagetable = (init32 >> 12) & ((1 << (21 - 12 + 1)) - 1);
unsigned long offset = (init32 >> 0 ) & ((1 << (11 - 0 + 1)) - 1);
如果这让您感到困惑,请查看 Google 上的“C 按位运算符”。你确实需要了解数字如何以二进制形式工作才能了解它的作用。