如何将两个有符号的位变量合并为一个有符号的位变量?

时间:2012-12-10 22:16:29

标签: c++ bit-manipulation bits

假设以下c ++代码:

#include <iostream>
using namespace std;

typedef struct 
{
       int a: 5;
       int b: 4;
  int c: 1;
  int d: 22;

} example;

int main()
{
example blah;

blah.a = -5; // 11011
blah.b = -3; // 1101

int result = blah.a << 4 | blah.b;

cout << "Result = " << result << endl; // equals 445 , but I am interested in this having a value of -67 

return 0;
}

我感兴趣的是变量result的类型为int,其中第9位是最高位。我希望这是这样的,所以结果= -67而不是445.这是怎么做到的?感谢。

0 个答案:

没有答案
相关问题