我是否使用按位移位正确提取这些字段? (标签、索引、偏移量)
我正在用 C 语言构建一个 CPU 缓存模拟器。我希望你能告诉我是否正确提取了这些字段:
32 位地址 应该分解如下:
+---------------------------------------------------+
| tag (20 bits) | index (10 bits) | offset (2 bits) |
+---------------------------------------------------+
这是我要获取的代码每个人的价值观:
void extract_fields(unsigned int address){
unsigned int tag, index, offset;
// Extract tag
tag = address >> 12;
// Extract index
index = address << 20;
index = index >> 22;
// Extract offset
offset = address << 30;
offset = offset >> 30;
}
非常感谢任何建议!
I am building a CPU cache emulator in C. I was hoping you could tell me if I am extracting these fields correctly:
The 32-bit address should be broken up as follows:
+---------------------------------------------------+
| tag (20 bits) | index (10 bits) | offset (2 bits) |
+---------------------------------------------------+
Here is my code to obtain the values for each:
void extract_fields(unsigned int address){
unsigned int tag, index, offset;
// Extract tag
tag = address >> 12;
// Extract index
index = address << 20;
index = index >> 22;
// Extract offset
offset = address << 30;
offset = offset >> 30;
}
Any advice is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的解决方案有效,但它通常以不同的方式完成,可能更清晰一些。
对于你的例子:
It looks like your solution works, but it is often done a different way that is probably a bit more clear.
For your example: