News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Bits fields in C++

Started by TouEnMasm, June 11, 2021, 06:21:59 PM

Previous topic - Next topic

TouEnMasm

Hello,

I am a little lost with the size of bit fields in c++ !
Quote
  struct ACLOSE_TRACK {                                                     // SIZE 10 bytes instead of 13
        UCHAR OperationCode;    // 0x5B - SCSIOP_CLOSE_TRACK_SESSION
        UCHAR Immediate : 1;
        UCHAR Reserved1 : 7;
        UCHAR Track     : 1;
        UCHAR Session   : 1;
        UCHAR Reserved2 : 6;
        UCHAR Reserved3;
        UCHAR TrackNumber[2];
        UCHAR Reserved4[3];
        UCHAR Control;
    };

struct Date {                             //SIZE 4 bytes
   unsigned short nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned short nMonthDay : 6;    // 0..31  (6 bits)
   unsigned short nMonth    : 5;    // 0..12  (5 bits)
   unsigned short nYear     : 8;    // 0..100 (8 bits)

What can explain the difference of Size from 13 to 10 in the ACLOSE_TRACK structure ?.


Fa is a musical note to play with CL

fearless

I believe OperationCode is 1 byte. Immediate and Reserved1 is 1 byte with bit 1 and bit 7 being defined. Track and session and Reserved2 is 1 byte with track and session being defined as the same bit. Reserved3 is 1 byte, TrackNumber[2] is 2 bytes, Reserved[3] is 3 bytes and Control is 1 byte. So that totals 10 bytes. I think.

TouEnMasm

Quote
Immediate and Reserved1 is 1 byte with bit 1 and bit 7 being defined
With rule is applied to do that ?

https://docs.microsoft.com/en-us/cpp/cpp/cpp-bit-fields?view=msvc-160

Rules seems to be
+bitn1 +bitn2 + ... < BIT FIELD SIZE declared
If not create a new BIT FIELD

Quote
UCHAR Immediate : 4;                                            // UCHAR = BIT FIELD SIZE declared = 8bits


Fa is a musical note to play with CL

TouEnMasm

#3
After further tests here the followed rules.

Quote
bit fields = bits of X A Y
container for  fields = 4,8,16,32,64-bit
Default size of the containing fields,int,4 bytes
Alignment by default int,8 bytes otherwise on size containing fields
Minimum size structure bit fields ,1 byte
Container size of accepted bit fields:
------  bool, char, char8_t, unsigned char, signed char, __int8   1 byte
------  char16_t, __int16, short, unsigned short, wchar_t, __wchar_t   2 bytes
------  char32_t, float, __int32, int, unsigned int, long, unsigned long   4 bytes
------  double, __int64, long double, long long, unsigned long long   8 bytes
Declaration:
The bits range from low to high weight. "field1:3,field2:4" gives "field1:bit 0 a 2",field2 bits 3 a 6
Creating new fields containing:
When the Sum of the preceding bits excess the defined size of the container or when a new size of container is used
alignment:
The alignment is done on the size of the bigger container or with a NULL bit field:0 (align DWORD)
A change in the size of the container  causes a realignement of the precedent bitfield
at the size of the new container
Fa is a musical note to play with CL

hutch--

I will leave this post here for the moment but could you keep thsi C++ code out of this sub forum, it is for assembler code only.

TouEnMasm

Ok,
Sorry for the trouble.
Fa is a musical note to play with CL

TouEnMasm

I have modify a little the rules verified  in a C++ forum.
Interest for masm is the translation of structure with bitfiels,they are numerous and seems to have changed since 2018.
The new version of headinc take care of this.
Fa is a musical note to play with CL

LiaoMi

Quote from: TouEnMasm on June 20, 2021, 03:53:37 PM
I have modify a little the rules verified  in a C++ forum.
Interest for masm is the translation of structure with bitfiels,they are numerous and seems to have changed since 2018.
The new version of headinc take care of this.

Example of correct and incorrect conversion:
headinc old
RECAACLOSE_TRACK RECORD     AAReserved2 : 6,
AASession   : 1,
AATrack     : 1,
AAReserved1 : 7,
AAImmediate : 1

ACLOSE_TRACK STRUCT DEFALIGNMASM
OperationCode BYTE ? ; 0x5B - SCSIOP_CLOSE_TRACK_SESSION
RECAACLOSE_TRACK BYTE ?
Reserved3 BYTE ?
TrackNumber BYTE 2 dup (?)
Reserved4 BYTE 3 dup (?)
Control BYTE ?
ACLOSE_TRACK ENDS
;+ IF > endif  Dйcompte des if: 0


headinc new
RECBACLOSE_TRACK RECORD RECBReserved2 : 6,
RECBSession : 1,
RECBTrack : 1,
RECBReserved1 : 7,
RECBImmediate : 1


ACLOSE_TRACK STRUCT DEFALIGNMASM
OperationCode BYTE ? ; 0x5B - SCSIOP_CLOSE_TRACK_SESSION
RECB db 2 dup(?)

Reserved3 BYTE ?
TrackNumber BYTE 2 dup (?)
Reserved4 BYTE 3 dup (?)
Control BYTE ?
ACLOSE_TRACK ENDS

;+ IF > endif  Dйcompte des if: 0
;Headinc 10.8 Windows Kits 10.0.19041.0


h2incX

ACLOSE_TRACK struct
  OperationCode BYTE ?
  ACLOSE_TRACK_??0 record Immediate_??0:1, Reserved1_??0:7, Track_??0:1, Session_??0:1, Reserved2_??0:6
  ACLOSE_TRACK_??0 <>
  Reserved3 BYTE ?
  TrackNumber BYTE 2 dup (?)
  Reserved4 BYTE 3 dup (?)
  Control BYTE ?
ACLOSE_TRACK ends

Date struct
  Date_??1 record nWeekDay_??1:3, nMonthDay_??1:6, nMonth_??1:5, nYear_??1:8
  Date_??1 <>
Date ends

;Errors: 0