473,809 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Type'ing the infamous 'flags' field

I write a lot of drivers and there is inevitably some hardware register or
buffer descriptor field called 'flags'. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don't think I can. Here's
what I was thinking:

typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};

static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;

But the problem is when combining flags:

struct HwThing dev;

dev.flags = flagA | flagB;

I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won't they? That is, I'd need to write:

dev.flags = (HwFlag) (flagA | flagB);

which rather defeats my intent. Any suggestions?

Thanks.

--
- Mark ->
--
Nov 14 '05
13 2070
In <Xn************ *************** *****@130.133.1 .4> "Mark A. Odell" <od*******@hotm ail.com> writes:
I suspect you did not tell Carsten to "engage his brain".


I didn't have to: he always does.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #11
In <84************ **************@ posting.google. com> ol*****@inspire .net.nz (Old Wolf) writes:
"Mark A. Odell" <od*******@hotm ail.com> wrote:

static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;


You should replace these '1's with '1UL'. For example if
int is 32bit and long is 64bit then 1 << 32 is undefined
behaviour.


As long as the shift count does not exceed 14 (and it doesn't in the
posted code), Mark's expressions are as correct and portable as you
can get. No need to replace 1 by anything else.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #12
Linus Torvalds <to******@osdl. org> wrote in
news:ci******** **@build.pdx.os dl.net:
Mark A. Odell wrote:

I write a lot of drivers and there is inevitably some hardware register
or buffer descriptor field called 'flags'. The flags are defined,
typically, as bit positions. I was thinking I could get some
compile-time type checking when assigning to a flag field but I don't
think I can.


I'll mention the tool we use for the Linux kernel, because some people
on comp.lang.c migth actually find it interesting.

It's called "sparse" (for "semantic parse"), and it extends the C type
system with various flags (much of them for checking "address spaces" of
pointers, since the kernel has to work with pointers that are in
distinct address spaces like "user" and "iomem"), and it also solves
your particular problem.

What you do is make a integer typedef that is restricted to "bitwise"
operations:

#ifdef __CHECKER__ /* Magic sparse flag */
#define __bitwise __attribute__(( bitwise))
#else
#define __bitwise
#endif

typedef unsigned int __bitwise cmd_flag_t;

...
#define CMD_ACTIVE ((cmd_flag_t) 0x1000)

[snip]

Thanks for the reply Linus. This looks very interesting. We use Diab's
tool chain (not gcc) but there may be some way to do this. Of course we're
moving off into non-ISO C but that may be my only choice.

Regards.

--
- Mark ->
--
Nov 14 '05 #13
On 16 Sep 2004 19:25:40 GMT, Chris Torek <no****@torek.n et> wrote
(reordered slightly for convenience):
In article <news:Xn******* *************** **********@130. 133.1.4>
Mark A. Odell <od*******@hotm ail.com> wrote:
I write a lot of drivers and there is inevitably some hardware
register or buffer descriptor field called 'flags' [, usually just
some individual bits]. I [wish I] could get some compile-time type
checking when assigning [named constants] to a flag field ...
Any suggestions?
Ada. :-)

Ada really does have just what you want, *and* is (partly) designed
for use in small embedded systems. In many ways it is a shame we
all use C to program them instead. :-)


Concur. And FWIW Ada has language-defined (standardized) interface to
C. And Fortran and COBOL, which are less likely to be useful in a
lowlevel hardware application. The GNU implementation GNAT also does
C++ (g++) and will "play fairly nicely" in a C main -- I don't know
about others -- but the OP said elsethread he isn't using GNU.
Seriously, the only way to get serious type-checking in C is to
make new types, using C's type-creation mechanism, the "struct".
This is not generally compatible with low-level bit-twiddling.

You can do clever stuff in C++ at compile time with templates, so
that the actual code emitted is just straight integer values going
into the bit-field. This is incredibly ugly, though.

To be clear you can't template a type as such; you would template an
"integer-like" class containing inlined methods, which would as you
say actually compile the same as integers but with added type checks.

Or in C++ you can just use enum types. Unlike C where enum types and
values are just (syntactic sugar for) integers, in C++ they are
distinct types for purposes of overloading and conversion although
they still behave like integer types, and you can't (accidentally)
convert *to* a (different) enum type without a cast. (And also, a
minor point, they can be as wide as long, if that is wider than int.)

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #14

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

Similar topics

1
26311
by: SD | last post by:
Hi, This is driving me nuts, I have a table that stores notes regarding an operation in an IMAGE data type field in MS SQL Server 2000. I can read and write no problem using Access using the StrConv function and I can Update the field correctly in T-SQL using: DECLARE @ptrval varbinary(16) SELECT @ptrval = TEXTPTR(BITS_data)
1
2189
by: Bill Strass | last post by:
I have a form showing the records of different people. I would like a photo to be displayed in the form, with each person's record. My digital photo > *.jpg type file and I change each photo to *.bmp format using SnagIt. I created an OLE type field in the table to store either the embedded photo or a link to the photo (I'll decide which method to use,
0
1622
by: Sandi | last post by:
I have a simple problem: I have an Access database (images.mdb) that has 2 columns: one is the id of the picture (an integer) and one (column named picture) is a field of type OLE Object which contains an image (it can store jpg, bmp, gif, but I don't know what image is stored inside). I want to retrieve the picture stored in the database and identified by a given id and display it in a web page (.aspx). I write in Visual C#, but it does...
3
18487
by: Ghost | last post by:
Hello. I have some problem to read Money Type Field from my database. I do so: I'm using SqlDataReader object to select some records and I have "float" type variable to store result. I write: myFloatVar = (float)mySqlDataReader; When this line executed the cast exception occurs.
4
3669
by: Robert Hanson | last post by:
Hi All, I am trying to add a record to a datatable that is connected to an Access database. I had no trouble with string and date fields, but for this record, I have two Long Integer field types and I get the following error: "Data type mismatch in criteria expression." I am using OleDbType.Integer as the type which matched the Int32 size for the Long Integer in Access, but I can't seem to get past the error.
3
7081
by: William Mild | last post by:
I am porting old Dreamweaver generated ASP code to ASP.NET (VBScript to VB.NET). Error: Cast from type 'Field' to type 'Integer' is not valid The error occurs on the line which says "rsStudents = Server.CreateObject("ADODB.Recordset")". if (Request.QueryString("StudentID") <> "") then rsStudents__MMColParam = Request.QueryString("StudentID")
0
1399
by: Simon | last post by:
Dear reader, I tray to insert a "bmp" or "jpg" picture in a table and than showing the picture in a report. I tray to fix this with the data type "Ole Object". In the "Ole Object"
1
2266
by: ritu raj shriwastaw | last post by:
I am using window xp and php trait(php3.1,mysql4.2).i have one field is of memo data type and I am unable to retrieve data from that memo data type field. what query I should write?
1
2706
by: rishiv | last post by:
Hi, I'm using MS Access 2002 SP3 & Visual Basic 6.3 on WinXP Home SP2. I have a table of condition data (5990 records) that relates to information stored in a proprietary application. The primary key in my table (MyKey) is the same as the key used to find data in the other windows application (C:\Folder\My.exe). So, if I am looking at a record in my table, say with ID = 100, I can view further information and images etc in my application...
0
9721
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
9602
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
10639
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...
0
10376
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 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...
1
10383
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
6881
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
5550
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...
1
4332
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
3861
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.