位字段问题 [英] Trouble with bit fields

查看:78
本文介绍了位字段问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我在处理位字段时遇到了一些麻烦。以下

是一个演示它的简单程序。


#include< iomanip>

#include< iostream>


struct instrucao_i

{

unsigned short opcode:6;

unsigned short rs:5 ;

unsigned short rt:5;

unsigned short immediate:16;

};


int main()

{

instrucao_i i = {0x24110064};

std :: cout<< std :: hex;

std :: cout<< 操作码: << i.opcode<< ''\ n'';

std :: cout<< " rs: << i.rs<< ''\ n'';

std :: cout<< " rt: << i.rt<< ''\ n'';

std :: cout<< immed。: << i.immediate<< ''\\ n';;

}


这是用于
$ b $的32位字的二进制表示b初始化/ i /:


0010 0100 0001 0001 0000 0000 0110 0100


由于/ opcode / field是6位长,因此应该等于/ i /的第一个

6位,即001001,即十进制的9。但是,这是我在Windows上使用VC ++ 7.1和BCC32 5.5.1获得的

输出:


D:\Temp> teste

操作码:24

rs:0

rt:0

immed。:0


有没有人能对这个问题有所了解?


非常感谢,


-

NeyAndrédeMello Zunino

Hello.

I have been having some trouble dealing with bit fields. The following
is a simple program that demonstrates it.

#include <iomanip>
#include <iostream>

struct instrucao_i
{
unsigned short opcode: 6;
unsigned short rs: 5;
unsigned short rt: 5;
unsigned short immediate: 16;
};

int main()
{
instrucao_i i = { 0x24110064 };
std::cout << std::hex;
std::cout << "opcode: " << i.opcode << ''\n'';
std::cout << " rs: " << i.rs << ''\n'';
std::cout << " rt: " << i.rt << ''\n'';
std::cout << "immed.: " << i.immediate << ''\n'';
}

Here is the binary representation of the 32-bit word being used to
initialize /i/:

0010 0100 0001 0001 0000 0000 0110 0100

Since the /opcode/ field is 6 bits long, it should be equal to the first
6 bits of /i/, i.e., 001001, which is 9 in decimal. However, this is the
output I get with both VC++ 7.1 and BCC32 5.5.1 on Windows:

D:\Temp>teste
opcode: 24
rs: 0
rt: 0
immed.: 0

Could anybody shed some light on this subject?

Thank you very much,

--
Ney André de Mello Zunino

推荐答案

"NeyAndrédeMello Zunino" <つ**** @ inf.ufsc.br>写道...
"Ney André de Mello Zunino" <zu****@inf.ufsc.br> wrote...
我在处理位字段时遇到了一些麻烦。以下
是一个演示它的简单程序。

#include< iomanip>
#include< iostream>

struct instrucao_i
{
unsigned short opcode:6;
unsigned short rs:5;
unsigned short rt:5;
unsigned short immediate:16;
};

int main()
{
instrucao_i i = {0x24110064};
std :: cout<< std :: hex;
std :: cout<< 操作码: << i.opcode<< ''\ n'';
std :: cout<< " rs: << i.rs<< ''\ n'';
std :: cout<< " rt: << i.rt<< ''\ n'';
std :: cout<< immed。: << i.immediate<< ''\ n'';
}

这是32位字的二进制表示,用于初始化/ i /:

0010 0100 0001 0001 0000 0000 0110 0100


编号这是你用来初始化i.opcode的32位字。

自/ opcode / field是6位长,它应该等于/ i /的第一个位,即001001,即十进制的9位。


为什么?初始化聚合的规则仍然适用。为了初始化一个结构,你需要提到所有元素。

然而,这是我用VC ++ 7.1和BCC32 5.5.1得到的
输出Windows:

D:\Temp> teste
操作码:24
rs:0
rt:0
immed。:0

有人可以对这个问题有所了解吗?
I have been having some trouble dealing with bit fields. The following
is a simple program that demonstrates it.

#include <iomanip>
#include <iostream>

struct instrucao_i
{
unsigned short opcode: 6;
unsigned short rs: 5;
unsigned short rt: 5;
unsigned short immediate: 16;
};

int main()
{
instrucao_i i = { 0x24110064 };
std::cout << std::hex;
std::cout << "opcode: " << i.opcode << ''\n'';
std::cout << " rs: " << i.rs << ''\n'';
std::cout << " rt: " << i.rt << ''\n'';
std::cout << "immed.: " << i.immediate << ''\n'';
}

Here is the binary representation of the 32-bit word being used to
initialize /i/:

0010 0100 0001 0001 0000 0000 0110 0100
No. It''s the 32-bit word you used to intialise i.opcode.
Since the /opcode/ field is 6 bits long, it should be equal to the first
6 bits of /i/, i.e., 001001, which is 9 in decimal.
Why? The rules for initialising aggregates still apply. In order
to initialise a struct you need all elements mentioned.
However, this is the
output I get with both VC++ 7.1 and BCC32 5.5.1 on Windows:

D:\Temp>teste
opcode: 24
rs: 0
rt: 0
immed.: 0

Could anybody shed some light on this subject?




结构的初始化是一件非常特别的事情。每个初始化程序

用于初始化相应的成员,如果初始化程序的成员数少于成员,则其余成员初始化为0.

在你的情况下,你使用0x24110064初始化''操作码''(它最后6位切断它的b / b
,产生24位),其余的为零。为什么

结果让你感到惊讶?如果你想要9'''操作码'',你应该写




instrucao_i i = {9,0,0x11,0x64};


Victor



Initialisation of a struct is a very particular thing. Each initialiser
is used to initialise the respective member, and if there are fewer
initialisers than members, the remaining members are initialised to 0.

In your case you intialise ''opcode'' with 0x24110064 (which cuts off its
last 6 bits, and yields 24), and the rest of them to zeroes. Why does
the result surprise you? If you wanted 9 in ''opcode'', you should have
written

instrucao_i i = { 9, 0, 0x11, 0x64 };

Victor


Victor Bazarov写道:


[...]
Victor Bazarov wrote:

[...]
在你的情况下,你使用0x24110064初始化''操作码''(它切断了它的最后6位,产生24位),其余的为零。为什么结果让你感到惊讶?如果你想要9'''操作码'',你应该写





i = {9,0,0x11,0x64};
In your case you intialise ''opcode'' with 0x24110064 (which cuts off its
last 6 bits, and yields 24), and the rest of them to zeroes. Why does
the result surprise you? If you wanted 9 in ''opcode'', you should have
written

instrucao_i i = { 9, 0, 0x11, 0x64 };




在我的真实程序中,位字段的实际用法略有不同。我只是试图简化它以便于理解

代码。


真实的主要区别代码是具有位

字段的结构与32位变量共享一个联合。所以,我实际上通过该数字初始化

结构。这是样本的扩展版本

程序:


#include< iomanip>

#include< iostream>


struct instrucao_i

{

unsigned short opcode:6;

unsigned short rs:5 ;

unsigned short rt:5;

unsigned short immediate:16;

};


union instrucao

{

unsigned int numero;

instrucao_i instr;

};


int main()

{

unsigned int numero = 0x24110064;

instrucao i;

i.numero = numero;

std :: cout<< std :: hex;

std :: cout<< 操作码: << i.instr.opcode<< ''\ n'';

std :: cout<< " rs: << i.instr.rs<< ''\ n'';

std :: cout<< " rt: << i.instr.rt<< ''\ n'';

std :: cout<< immed。: << i.instr.immediate<< ''\\ n'';

}


这就是我将32位数字输入结构的方法。我的意图是

位字段是为了能够访问指令的每一部分

pack。我还缺少什么?


再次感谢你,


-

NeyAndrédeMello Zunino



The actual usage of the bit fields varies a little in my real program. I
only tried to simplify it in order to facilitate the comprehension of
the code.

The main difference in the real code is that the struct with the bit
fields shares a union with a 32-bit variable. So, I actually initialize
the struct via that number. Here is an extended version of the sample
program:

#include <iomanip>
#include <iostream>

struct instrucao_i
{
unsigned short opcode: 6;
unsigned short rs: 5;
unsigned short rt: 5;
unsigned short immediate: 16;
};

union instrucao
{
unsigned int numero;
instrucao_i instr;
};

int main()
{
unsigned int numero = 0x24110064;
instrucao i;
i.numero = numero;
std::cout << std::hex;
std::cout << "opcode: " << i.instr.opcode << ''\n'';
std::cout << " rs: " << i.instr.rs << ''\n'';
std::cout << " rt: " << i.instr.rt << ''\n'';
std::cout << "immed.: " << i.instr.immediate << ''\n'';
}

That is how I get the 32-bit number into the struct. My intention with
the bit field is to be able to access each part of the instruction''s bit
pack. What am I still missing?

Thank you again,

--
Ney André de Mello Zunino


NeyAndrédeMello Zunino写道:
Ney André de Mello Zunino wrote:
实际代码的主要区别在于具有位的结构
fields与32位变量共享一个联合。所以,我实际上通过该数字初始化了结构。这是一个示例
程序的扩展版本:
The main difference in the real code is that the struct with the bit
fields shares a union with a 32-bit variable. So, I actually initialize
the struct via that number. Here is an extended version of the sample
program:




我忘了包含新版本的输出,这是

仍然不是我想要的:


D:\ Temp> teste

操作码:24

rs :1

rt:0

immed。:2411


问候,


-

NeyAndrédeMello Zunino



I forgot to include the output I get with the new version, which is
still not what I am looking for:

D:\Temp>teste
opcode: 24
rs: 1
rt: 0
immed.: 2411

Regards,

--
Ney André de Mello Zunino


这篇关于位字段问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆