Trouble with bit fields

From: Ney André de Mello Zunino (zunino_at_inf.ufsc.br)
Date: 08/18/04


Date: Wed, 18 Aug 2004 00:58:53 -0300

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