Results 1 to 6 of 6

Thread: What do the colon and number mean in this struct definition?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    What do the colon and number mean in this struct definition?

    I was looking at this one struct definition in the Windows API, and several of the fields in the struct have a colon and a number after the field type and field name. What does that colon and number mean?

    https://learn.microsoft.com/en-us/wi...inbase-comstat

    Code:
    typedef struct _COMSTAT {
      DWORD fCtsHold : 1;
      DWORD fDsrHold : 1;
      DWORD fRlsdHold : 1;
      DWORD fXoffHold : 1;
      DWORD fXoffSent : 1;
      DWORD fEof : 1;
      DWORD fTxim : 1;
      DWORD fReserved : 25;
      DWORD cbInQue;
      DWORD cbOutQue;
    } COMSTAT, *LPCOMSTAT;

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: What do the colon and number mean in this struct definition?

    COOL! I didn't know you could address individual bits in any programming language. I thought you needed to use bitwise arithmetic to isolate single bits or sets of bits within a byte. In Visual Basic 6 for example, to get the 5th bit (bit4) you would do:
    Code:
    'for a 1-bit bitfield starting on the 5th bit
    bf1 = (bitfields\16) And 1
    
    ;for a 2-bit bitfield starting on the 5th bit
    bf2 = (bitfields\16) And 3
    Last edited by Ben321; Nov 19th, 2022 at 06:48 PM. Reason: fixed typo

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: What do the colon and number mean in this struct definition?

    Note that this c/c++ method for individual bits can only be used within a struct/class. Also, you have to be careful of endian layout and padding - especially if you're trying to match to eternal data.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: What do the colon and number mean in this struct definition?

    Quote Originally Posted by Ben321 View Post
    COOL! I didn't know you could address individual bits in any programming language. I thought you needed to use bitwise arithmetic to isolate single bits or sets of bits within a b. In Visual Basic 6 for example, to get the 5th bit (bit4) you would do:
    Code:
    'for a 1-bit bitfield starting on the 5th bit
    bf1 = (bitfields\16) And 1
    
    ;for a 2-bit bitfield starting on the 5th bit
    bf2 = (bitfields\16) And 3
    Look here (Freepascal/Delphi): https://www.freepascal.org/docs-html...ses/tbits.html
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: What do the colon and number mean in this struct definition?

    I'd add to this a note that if you're translating these to VB for use with APIs,

    Code:
    Public Type COMSTAT
        dwBitfield As Long
        cbInQueue As Long
        cbOutQueue As Long
    End Type
    You add up the bits; they're not separate variables of which only 1 bit is used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width