本文介绍了如何为位掩码创建48位uint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建48位整数值.我知道可以使用char数组或结构,但是我希望能够进行位掩码/操作,但我不确定该怎么做.

I am trying to create a 48-bit integer value. I understand it may be possible to use a char array or struct, but I want to be able to do bit masking/manipulation and I'm not sure how that can be done.

当前程序使用16位uint,我需要将其更改为48.这是一个字节码解释器,我想将内存寻址扩展到4GB.我可以只使用64位,但这会浪费很多空间.

Currently the program uses a 16-bit uint and I need to change it to 48. It is a bytecode interpreter and I want to expand the memory addressing to 4GB. I could just use 64-bit, but that would waste a lot of space.

以下是代码示例:

unsigned int program[] = { 0x1064, 0x11C8, 0x2201, 0x0000 };

void decode( )
{
  instrNum = (program[i] & 0xF000) >> 12; //the instruction
  reg1     = (program[i] & 0xF00 ) >>  8; //registers
  reg2     = (program[i] & 0xF0  ) >>  4;
  reg3     = (program[i] & 0xF   );
  imm      = (program[i] & 0xFF  ); //pointer to data
}

完整程序: http://en.wikibooks.org/wiki/Creating_a_Virtual_Machine/Register_VM_in_C

推荐答案

您可以使用位字段,这些位字段通常用于表示已知的固定位宽的整数类型.位域的一种众所周知的用法是表示一组位和/或一系列位,称为标志.您可以对其应用位操作.

You can use the bit fields which are often used to represent integral types of known, fixed bit-width. A well-known usage of bit-fields is to represent a set of bits, and/or series of bits, known as flags. You can apply bit operations on them.

#include <stdio.h>
#include <stdint.h>

struct uint48 {
    uint64_t x:48;
} __attribute__((packed));

这篇关于如何为位掩码创建48位uint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:41