本文介绍了运营商在C:的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:结果
  结果
  

大家好,我讨厌问这样的问题,但它确实窃听我,所以我会问:

Hello everyone, I hate to ask this type of question, but it's really bugging me, so I will ask:

什么是功能:操作者在下面的code

What is the function of the : operator in the code below?

#include <stdio.h>

struct microFields
{
  unsigned int addr:9;
  unsigned int cond:2;
  unsigned int wr:1;
  unsigned int rd:1;
  unsigned int mar:1;
  unsigned int alu:3;
  unsigned int b:5;
  unsigned int a:5;
  unsigned int c:5;
};

union micro
{
  unsigned int microCode;
  microFields code;
};

int main(int argc, char* argv[])
{
  micro test;
  return 0;
}

如果有人关心可言,我把这个code从下面的链接:
http://www.cplusplus.com/forum/beginner/15843/

If anyone cares at all, I pulled this code from the link below:http://www.cplusplus.com/forum/beginner/15843/

我真的很想知道,因为我知道我以前在什么地方见过这一点,我想了解它,当我再次看到它。

I would really like to know because I know I have seen this before somewhere, and I want to understand it for when I see it again.

谢谢!

推荐答案

他们是位字段,一个例子是那个无符号整型地址:9; 创建一个地址字段9位。

They're bit-fields, an example being that unsigned int addr:9; creates an addr field 9 bits long.

它通常用于包装大量的价值为一体的类型。在特定情况下,它定义了一个(可能)假设CPU的32位微code指令结构(如果你加起来所有的位字段长度,它们之和为32)。

It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).

工会可以让你在一个单一的32位值加载,然后访问与code类的各个字段(固定以及小问题, code 和测试

The union allows you to load in a single 32-bit value and then access the individual fields with code like (minor problems fixed as well, specifically the declarations of code and test):

#include <stdio.h>

struct microFields {
    unsigned int addr:9;
    unsigned int cond:2;
    unsigned int wr:1;
    unsigned int rd:1;
    unsigned int mar:1;
    unsigned int alu:3;
    unsigned int b:5;
    unsigned int a:5;
    unsigned int c:5;
};

union micro {
    unsigned int microCode;
    struct microFields code;
};

int main (void) {
    int myAlu;
    union micro test;
    test.microCode = 0x0001c000;
    myAlu = test.code.alu;
    printf("%d\n",myAlu);
    return 0;
}

这打印出7,这是三位组成位字段。

This prints out 7, which is the three bits making up the alu bit-field.

这篇关于运营商在C:的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:23