473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with bit fields

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
Jul 22 '05 #1
12 2179
"Ney André de Mello Zunino" <zu****@inf.ufsc.br> wrote...
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?


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
Jul 22 '05 #2
Victor Bazarov wrote:

[...]
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 };


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
Jul 22 '05 #3
Ney André de Mello Zunino wrote:
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:


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
Jul 22 '05 #4
Hi

In VC++, the order of your struct members needs to be reversed as
follows:

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

Then the output you get is as follows:
opcode: 9
rs: 0
rt: 11
immed.: 64

Hope this helps ...
Ashley
Jul 22 '05 #5
Hi

Oops, made typo in previous post - struct declaration was meant to be as
follows:

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

Apologies for that ...
Ashley

Jul 22 '05 #6
Ashes wrote:
struct instrucao_i
{
unsigned short immediate: 16;
unsigned short rt: 5;
unsigned short rs: 5;
unsigned short opcode: 6;
};


Thanks, it works. But I noticed that if I change the type of the last
three fields above from unsigned short to unsigned char (which is enough
to hold those fields), the behavior gets wrong again. Why is that? And
why must the members be laid out in the opposite order? Is that only the
case for IA32-based machines?

Regards,

--
Ney André de Mello Zunino
Jul 22 '05 #7
Ney André de Mello Zunino <zu****@inf.ufsc.br> wrote:
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';
}


The value of 'i.instr' is undefined: you initialized 'i.numero'. After
the corresponding assignment you can read an 'i.numero' and expect it
to contain the value you designed to it. What happens if you access
'i.instr' without prio assignment to this field is undefined.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #8
Ney André de Mello Zunino wrote:
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,


I believe that you should try a different approach.
Use the bitwise arithmetic operators to isolate the
bit fields into integral variables:

class Instruction
{
public:
Instruction(unsigned long data = 0);
private:
unsigned int opcode;
unsigned int rs;
unsigned int rt;
unsigned int immediate;
};

Instruction::Instruction(unsigned long data)
{
immediate = static_cast<unsigned int>(data & 0xFF);
data >>= 16;
rt = static_cast<unsigned int>(data & 0x1F);
data >>= 5;
rs = static_cast<unsigned int>(data & 0x1F);
data >>= 5;
opcode = static_cast<unsigned int>(data & 0x3F);
}

Not using bit fields allows for easier conversion from
Big Endian or Little Endian byte ordering. Also, some
compilers mess up when accessing bitfields in
structures, especially when the optimization level
is set high.

Another nice feature is that bit fields are converted
into the processor's native integral format. This
will simplify your program and make it more efficient
(faster).

You _may_ want to consider a hierarchial approach, in
which Instruction is a base class and have separate
leaf classes for each instruction. This will allow
generic functions, such as execute and print, to
be applied to any instruction. I used an array of
pointers to the Instruction base class to represent
a program.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #9
Hi

On 32 bit Windows, the size of an unsigned char is 1 byte whereas the size
of an unsigned short is 2 hence the difference when changing the type.

I believe that the reason the struct has to be reversed to get the byte
ordering you need is Microsoft compiler specific.

Regards
Ashley

Jul 22 '05 #10

"Ashes" <av**************@nospam.hotmail.com> wrote in message

I believe that the reason the struct has to be reversed to get the byte
ordering you need is Microsoft compiler specific.


Bitfields are pretty worthless if you care about the packing. It's implementation
dependent everywhere. I had no fewer than 4 different definitions of a bitfield
back when I was using it to pick out bits of a network packet. I finally just gave
up and used masks and shifts instead.

Jul 22 '05 #11
Ron Natalie wrote:
"Ashes" <av**************@nospam.hotmail.com> wrote in message
I believe that the reason the struct has to be reversed to get the byte
ordering you need is Microsoft compiler specific.

Bitfields are pretty worthless if you care about the packing. It's implementation
dependent everywhere. I had no fewer than 4 different definitions of a bitfield
back when I was using it to pick out bits of a network packet. I finally just gave
up and used masks and shifts instead.


Actually, the problem isn't so much related to the platform
but with the compiler. I've had different compilers change
the ordering and also screw up on accessing the bit fields.
I just save myself the agony and use masks and shifts. This
is more portable.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #12
Thomas Matthews wrote:
Actually, the problem isn't so much related to the platform
but with the compiler. I've had different compilers change
the ordering and also screw up on accessing the bit fields.
I just save myself the agony and use masks and shifts. This
is more portable.


Ok, I think I've read enough about those nasty bit fields. I guess I
will just join most of you and go the way of bit shifting. Nevertheless,
I must say I would rather see compilers improve in this regard than
have a unreliable feature lying around.

Thanks to all,

--
Ney André de Mello Zunino
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2574
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
1
1511
by: Chris Todhunter | last post by:
Hi, I am responsible for a growing website. It is password protected and we collect data on everyone who logs in. Every month we produce a report on the trends of users which can result in some...
2
2510
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2)....
2
2674
by: Joey P | last post by:
Hi all, I am doing a project for university whereby i have to implement a simple database related to a frozen foods company. I am having some trouble though creating a validation rule for one...
3
1854
by: fstenoughsnoopy | last post by:
Ok the complete story. I have a Contact Table, Query and Form, that are used to input and store the contact info for customers. They have FirstName, LastName and Address as the primary key...
0
1054
by: Bomac8 | last post by:
I have a text file with a four field array like this: DET-01-002737,DET-01-002737,YES,64239764b32fefc915a593f41bdb5730. My program sorts them in the order where the 3rd field says "Yes" or "NO". ...
1
1652
by: Tableshavturned | last post by:
Hi this is my first post on the forums. I haven't really developed before with Access 2003 so trouble shooting with this application is not my forte. The issue at hand is, created a star schema with...
3
2188
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
5
2424
matheussousuke
by: matheussousuke | last post by:
Hi guys, good morning. I've just get this script for converting mysql tables from wordpress, and I want to use it in my server, but no with wordpress, with oscommerce, a friend of mine told me a...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.