473,395 Members | 1,968 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,395 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 20 '05 #1
1 3564
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 20 '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);
10
by: junky_fellow | last post by:
I am trying to print the offset of a particulat member in a structure, but it's not working. I am using the following expression to print the offset. &(struct my_struct *)0->member_name ...
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; }...
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...
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...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.