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 12 2218
"Ney André de Mello Zunino" <zu****@inf.ufs c.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
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.immedia te << '\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
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
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
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
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
Ney André de Mello Zunino <zu****@inf.ufs c.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.immedia te << '\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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
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(uns igned long data = 0);
private:
unsigned int opcode;
unsigned int rs;
unsigned int rt;
unsigned int immediate;
};
Instruction::In struction(unsig ned long data)
{
immediate = static_cast<uns igned int>(data & 0xFF);
data >>= 16;
rt = static_cast<uns igned int>(data & 0x1F);
data >>= 5;
rs = static_cast<uns igned int>(data & 0x1F);
data >>= 5;
opcode = static_cast<uns igned 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.l earn.c-c++ faq: http://www.comeaucomputing.com/learn/faq/
Other sites: http://www.josuttis.com -- C++ STL Library book
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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!
|
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
|
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...
|
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...
| |
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:
...
|
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
|
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...
|
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...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |