473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Assign bit offset in a struct

J
I'm interfacing with a C api (via Interop) which uses the following
typedef struct...

typedef struct _columnflags
{
BYTE bNoUpdate : 1;
BYTE bSetToNull : 1;
BYTE bDefault : 1;
} columnflags;

FYI: For those unfamiliar with this C/C++ syntax, the ": 1" specifies a
bit offset of 1 within the (8-bit) Byte. So bit 1 represents the
variable "bNoUpdate", bit 2 represents "bSetToNull", and bit 3
represents "bDefault". The remaining 5 bits are not used/referenced and
the total size of the structure is 1 Byte (8 bits).
I've been trying to declare this struct in VB.Net without success and
have had no luck researching news groups and other online resources.

In the meantime, I've tried approaches such as...

<StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Ansi)> _
Public Structure columnflags
Public bNoUpdate As Byte
Public bSetToNull As Byte
Public bDefault As Byte
End Structure

.... which just creates a structure of 3 Bytes instead 1 Byte with 3 bit
fields.

I've also tried...

<StructLayout(LayoutKind.Explicit, Size:=1, Pack:=4,
CharSet:=CharSet.Ansi)> _
Public Structure columnflags
<FieldOffset(1)> Public bNoUpdate As Byte
<FieldOffset(2)> Public bSetToNull As Byte
<FieldOffset(3)> Public bDefault As Byte
End Structure

....however, the FieldOffset attribute is specified in Bytes (not Bits).

Ideally, there would be an alternate attribute to FieldOffset that could
take in an argument representing the number of bits instead of number of
bytes.

If anyone has tackled a similar problem or has a solution to this I
would greatly appreciate it.

Thanks for your time. - J.

Nov 16 '05 #1
1 2232
Pass the bit-packed struct as a single variable of type BYTE and use bit-and
and bit-or to test/set/clear the individual bits. AFIAK, the CLR has no
concept of a bit-sized field.

-cd

J wrote:
I'm interfacing with a C api (via Interop) which uses the following
typedef struct...

typedef struct _columnflags
{
BYTE bNoUpdate : 1;
BYTE bSetToNull : 1;
BYTE bDefault : 1;
} columnflags;

FYI: For those unfamiliar with this C/C++ syntax, the ": 1" specifies
a bit offset of 1 within the (8-bit) Byte. So bit 1 represents the
variable "bNoUpdate", bit 2 represents "bSetToNull", and bit 3
represents "bDefault". The remaining 5 bits are not used/referenced
and the total size of the structure is 1 Byte (8 bits).
I've been trying to declare this struct in VB.Net without success and
have had no luck researching news groups and other online resources.

In the meantime, I've tried approaches such as...

<StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Ansi)>
_ Public Structure columnflags
Public bNoUpdate As Byte
Public bSetToNull As Byte
Public bDefault As Byte
End Structure

... which just creates a structure of 3 Bytes instead 1 Byte with 3
bit fields.

I've also tried...

<StructLayout(LayoutKind.Explicit, Size:=1, Pack:=4,
CharSet:=CharSet.Ansi)> _
Public Structure columnflags
<FieldOffset(1)> Public bNoUpdate As Byte
<FieldOffset(2)> Public bSetToNull As Byte
<FieldOffset(3)> Public bDefault As Byte
End Structure

...however, the FieldOffset attribute is specified in Bytes (not
Bits).

Ideally, there would be an alternate attribute to FieldOffset that
could take in an argument representing the number of bits instead of
number of bytes.

If anyone has tackled a similar problem or has a solution to this I
would greatly appreciate it.

Thanks for your time. - J.

Nov 16 '05 #2

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

Similar topics

4
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
3
by: Arto Huusko | last post by:
Hello, I'm wondering about the portability and correctness of the following scenario. I have a generic doubly linked list structure, and it contains generic nodes that look like this: ...
4
by: __jakal__ | last post by:
Hello, I need to find out the time difference between UTC and local time. I am doing it the following way #include <sys/timeb.h> #include <stdio.h> int main() { struct timeb tp; ftime(&tp);
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
1
by: J | last post by:
I'm interfacing with a C api (via Interop) which uses the following typedef struct... typedef struct _columnflags { BYTE bNoUpdate : 1; BYTE bSetToNull : 1; BYTE bDefault : 1; }...
24
by: funkyj | last post by:
PROBLEM STATEMENT: I want to calculate the byte offset of a field with a struct at compile time without instantiating an instance of the struct at runtime AND I want to do this in an ANSI standard...
19
by: Rahul | last post by:
Hi, Is there a way to find the offset of a class member at compile time. e.g. class A{ int i; int j; char c; }; Here the offset of c = 8 bytes from the start of an object of A (assuming 4...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.