Author Topic: Bits fields in C++  (Read 663 times)

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Bits fields in C++
« on: June 11, 2021, 06:21:59 PM »
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

  • Member
  • ****
  • Posts: 515
    • Github
Re: Bits fields in C++
« Reply #1 on: June 11, 2021, 07:31:33 PM »
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.
fearless

Lian Li PC-O11DW Case, ASUS ROG Crosshair VIII Hero WiFi, AMD Ryzen 9 5950X, 32GB DDR4-3600MHz, MSI RX 5700XT Gaming X, NZXT Kraken Z73 360mm Cooler, Seasonic PRIME 1000W PSU

My Github  Twitter

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Bits fields in C++
« Reply #2 on: June 11, 2021, 10:34:34 PM »
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

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Bits fields in C++
« Reply #3 on: June 12, 2021, 05:47:53 PM »
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
« Last Edit: June 20, 2021, 03:49:55 PM by TouEnMasm »
Fa is a musical note to play with CL

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8497
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Bits fields in C++
« Reply #4 on: June 12, 2021, 05:56:31 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Bits fields in C++
« Reply #5 on: June 12, 2021, 10:49:19 PM »
Ok,
Sorry for the trouble.
Fa is a musical note to play with CL

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Bits fields in C++
« Reply #6 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.
Fa is a musical note to play with CL

LiaoMi

  • Member
  • ****
  • Posts: 922
Re: Bits fields in C++
« Reply #7 on: June 20, 2021, 06:47:07 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
Code: [Select]
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
Code: [Select]
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

Code: [Select]
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