Results 1 to 8 of 8

Thread: [RESOLVED] How to deal with bitfields with an odd number of bits?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    3,705

    Resolved [RESOLVED] How to deal with bitfields with an odd number of bits?

    I'm debating how to declare the following structure in VB:

    Code:
    #pragma pack(4)
    typedef struct _tableCellParms
    {							// EM_INSERTTABLE lparam is a (TABLECELLPARMS *)
    	LONG	dxWidth;		// Cell width (\cellx)
    	WORD	nVertAlign:2;	// Vertical alignment (0/1/2 = top/center/bottom
    	//	\clvertalt (def), \clvertalc, \clvertalb)
    	WORD	fMergeTop:1;	// Top cell for vertical merge (\clvmgf)
    	WORD	fMergePrev:1;	// Merge with cell above (\clvmrg)
    	WORD	fVertical:1;	// Display text top to bottom, right to left (\cltxtbrlv)
    	WORD	fMergeStart:1;	// Start set of horizontally merged cells (\clmgf)
    	WORD	fMergeCont:1;	// Merge with previous cell (\clmrg)
    	WORD	wShading;		// Shading in .01%		(\clshdng) e.g., 10000 flips fore/back
    
    	SHORT	dxBrdrLeft; 	// Left border width	(\clbrdrl\brdrwN) (in twips)
    	SHORT	dyBrdrTop;		// Top border width 	(\clbrdrt\brdrwN)
    	SHORT	dxBrdrRight;	// Right border width	(\clbrdrr\brdrwN)
    	SHORT	dyBrdrBottom;	// Bottom border width	(\clbrdrb\brdrwN)
    	COLORREF crBrdrLeft;	// Left border color	(\clbrdrl\brdrcf)
    	COLORREF crBrdrTop; 	// Top border color 	(\clbrdrt\brdrcf)
    	COLORREF crBrdrRight;	// Right border color	(\clbrdrr\brdrcf)
    	COLORREF crBrdrBottom;	// Bottom border color	(\clbrdrb\brdrcf)
    	COLORREF crBackPat; 	// Background color 	(\clcbpat)
    	COLORREF crForePat; 	// Foreground color 	(\clcfpat)
    } TABLECELLPARMS;
    The bitfield is only 7 bits, but declared as a WORD (2 bytes), in a structure subject to 4-byte packing... would I declare it as a single byte, or 2 bytes, and what would the offset of wShading need to be?

  2. #2

  3. #3
    Fanatic Member
    Join Date
    Jun 2015
    Posts
    848

    Re: How to deal with bitfields with an odd number of bits?

    When in doubt write a small c console program and inspect memory with test values or a dll for use with vb so you can pass the data back and forth and make sure you are interoperating properly. seeing it in action will quickly tell you what needs to be done
    Last edited by dz32; Nov 6th, 2022 at 09:01 AM.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    3,705

    Re: How to deal with bitfields with an odd number of bits?

    Thanks guys.

    And yeah I've compiled C programs to print out sizeofs and offsets before, but that's the nuclear option, I figured someone would know offhand rather than spend an hour doing that

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,483

    Re: How to deal with bitfields with an odd number of bits?

    Quote Originally Posted by fafalone View Post
    Thanks guys.

    And yeah I've compiled C programs to print out sizeofs and offsets before, but that's the nuclear option, I figured someone would know offhand rather than spend an hour doing that :)
    I keep a similar project open in VS at all times. Itís impossible for me to program in VB6 without printing correct sizeofs and offsetofs nowadays :))

  6. #6
    Fanatic Member
    Join Date
    Jun 2015
    Posts
    848

    Re: [RESOLVED] How to deal with bitfields with an odd number of bits?

    I tend to start with this approach as well if I have any question at all. A few minutes to get a concrete answer saves hours down the line.

    It can get answers in literally a minute or two though.
    Last edited by dz32; Nov 6th, 2022 at 09:00 AM.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    3,705

    Re: [RESOLVED] How to deal with bitfields with an odd number of bits?

    For kernel mode there's the Vergilius Project... awesome resource. Need something like that for the user mode Windows API.

  8. #8
    Fanatic Member
    Join Date
    Jun 2015
    Posts
    848

    Re: [RESOLVED] How to deal with bitfields with an odd number of bits?

    Good link thanks

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