473,765 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

8 bit bit field?

I have an 8 bit byte in which two bits are used and the remaining 6 are
unused.

#pragma pack(1)
typedef struct
{
unsigned int item_len;
unsigned char presentationCon textID;
unsigned char unused : 6;
unsigned char command : 1;
unsigned char last : 1;
} pdv_type;

Does this syntax look correct?
unused bits 7,6,5,4,3,2
command bit 1
last bit 0

Will the compiler try to put the bit field into a 16 or 32 bit holder?

Feb 27 '06 #1
2 2275
On 27 Feb 2006 11:40:42 -0800, "JustSomeGu y" <bo*******@yaho o.com>
wrote:
I have an 8 bit byte in which two bits are used and the remaining 6 are
unused.

#pragma pack(1)
typedef struct
{
unsigned int item_len;
unsigned char presentationCon textID;
unsigned char unused : 6;
unsigned char command : 1;
unsigned char last : 1;
} pdv_type;

Does this syntax look correct?
unused bits 7,6,5,4,3,2
command bit 1
last bit 0

Will the compiler try to put the bit field into a 16 or 32 bit holder?


#include <iostream>
#include <ostream>

#pragma pack(1)
typedef struct
{
unsigned int item_len;
unsigned char presentationCon textID;
unsigned char unused : 6;
unsigned char command : 1;
unsigned char last : 1;
} pdv_type;

int main() {
using namespace std;
cout << "Size of pdv_type: " << sizeof(pdv_type ) << endl;
return 0;
}

--
Bob Hairgrove
No**********@Ho me.com
Feb 27 '06 #2
JustSomeGuy wrote:
I have an 8 bit byte in which two bits are used and the remaining 6 are
unused.

#pragma pack(1) ^^^^^^^^^^^^^^^
This is implementation-defined.
typedef struct
{
unsigned int item_len;
unsigned char presentationCon textID;
unsigned char unused : 6;
unsigned char command : 1;
unsigned char last : 1;
} pdv_type;
You're not in C any more, just use normal C++ syntax:

struct pdv_type
{
...
};

Does this syntax look correct?
unused bits 7,6,5,4,3,2
command bit 1
last bit 0

Will the compiler try to put the bit field into a 16 or 32 bit holder?


Allocation and alignment are implementation-defined. If you want to
make sure that it's bits 0 and 1 that you're using and the allocation is
actually 1 byte, you need to write

unsigned char mybitstuff;

bool command() const { return mybitstuff & 1; }
bool last() const { return (mybitstuff >> 1) & 1; }
void set_command(boo l b)
{ if (b) mybitstuff |= 1; else mybitstuff &= ~1; }
void set_last(bool b)
{ if (b) mybitstuff |= 2; else mybitstuff &= ~2; }

i.e. make the actual things _an_interface_ instead of naked data.

V
--
Please remove capital As from my address when replying by mail
Feb 27 '06 #3

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

Similar topics

3
12970
by: BlackFireNova | last post by:
This concerns an Access 2002 (XP) database. There are two fields, "Notes" (Memo Field) and "Notes Update" on a form (among others) which I am concerned with here. Problem: I need to be able to tell when people have added notes to a record. (this database contains 6000+ records) I want to set it up so that "Notes Update" is updated to the current date whenever someone actually enters data into, or modifies data in, the "Notes"...
5
6111
by: ND | last post by:
I need to create a separate field from 4 fields, "street address", "city", "State" and "zip code". For example, Street address - 100 Forest Street City - Seattle State - WA Zip - 05555 Would become:
26
2974
by: temp | last post by:
Hi, My boss is asking me to generate a column mapping report of all the queries. Basically, we get our data from ORACLE. There's a queary that create new table from ORACLE tables. Then, there are reports and queries that uses the new table. Is there an add in or tool that can generate mapping reports of the queries.
18
18417
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
2
19609
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the field in table2. What I have is a form that is linked to Table2. If the users want to change a field in the main database (table1), they fill the change in to the form (which is linked to table2) If there is no change to the field, they simply...
9
3137
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to be inserted into a standard web address in the table (the filed name is link) in ddw1 Example address ---
13
14401
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on the input field. Any help? Please??? I don't get any error on my javascript console in firefox, and I am not seeing any errors in IE. // makes an input field that submits itself using setCalendarValue() when it's blurred (it will be blurred if...
9
9699
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
1
4341
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same logic for a date field. i.e. theValue.defaultvalue = "#" & theValue.value & "#" I can't get this to work for some reason and I can't figure out why. Can anyone out there help? Thanks Repeat value of previous record field into current...
3
5647
by: klbachrodt | last post by:
Hi all - I've been browsing this forum for a while now, looking for help, and finally decided to join and ask my question, since I'm not terribly good at writing SQL queries in Access. Here is my dilemma: Table U contains 1 field (Field F, primary key = no dupes) Table N contains 50 fields (including Field F, same as in Table U) * Field F in Table N contains multiple records, with dupe values Would like to create a new Table (or just...
0
9568
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, 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...
0
9399
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,...
0
10161
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, 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...
1
9955
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,...
0
6649
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
5275
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
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
3
2806
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.