473,789 Members | 2,662 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
12 2219

"Ashes" <av************ **@nospam.hotma il.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.hotma il.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.l earn.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
2590
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 access the recordset it displays the results, what the real issue is that the entry is not made into the database even though i use the Update command and i have also tried the BeginTrans and CommitTrans nothign seems to work and i am unable to...
1
1522
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 very unwieldy sql statements. We are running MYSQL with SunOne ASP on a Solaris box. Can anyone help with a way of improving the script - this currently takes about a minute to complete!
2
2530
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). It saves the data if I type it in manually to an html file. But it won't do that if I use the URL notation above. How do I get it to do this. The HTML for the form is below. Thank in advance, ed
2
2691
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 of my fields. I have a table called "Product" and two of the fields included in this table are "Cost Price" and "Retail Price". I need to create a validation rule so that the Cost Price is always less than the Retail Price. I have tried...
3
1877
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 fields to keep out duplicates. I am trying to put a full name box on the query, that uses the FirstName, LastName and Middle Initial and puts them together to form a full name. That I have so far. On my order database(consisting of a master table with...
0
1094
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". "YES" means it isa parent, "NO" means it is the child of that parent as long as field 2 of the parent is equal to field 2 of the child. Forn some reason my code is printing some of the children multiple times. example of some of the output: ...
1
1680
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 all the IDs in one table for the eight tables I created in design view, the datatype for all the IDs is number in this Table. ID EventID ObsID RecID ActItemID LocID AssgnID ImpctID ProjID
3
2213
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 that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another menu appears with options based on that city. I can't seem to get the input from these fields in the...
5
2441
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 few things I should do for make it working, but less than a rookie in PHP, began studying it now ¬¬", and sure I dont know how to use command line yet, she told me something about using command line along with FTP ascii table, I'm lost, couldn't do...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.