CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    22

    Structure containing bit mask

    I'm parsing a byte[] to a structure using StructLayout. I've managed to get this to work for integers, arrays, etc but I need to check for bits too. So something like this in C++.

    Code:
          struct
          {
             unsigned short  one     : 6;
             unsigned short  two     : 5;
             unsigned short  three   : 5;      
          } field;
    I guess I need to use BitArray but I've been unbale to get this to work with StructLayout.

    Could anyone point me in the correct direction?
    Thank you.

  2. #2
    Join Date
    Apr 2006
    Posts
    220

    Re: Structure containing bit mask

    There is not concept of Bit mask in C# as there is in C++; Let someone correct me if I am wrong.

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Structure containing bit mask

    Are you going to handle data between managed and unmanaged code so you need to use StructLayout ? Whats the exact problem you want to get handled with this?

    Basically if you need to use a bitmask you can do it in the properties
    of the struct
    Code:
    private int data;
    public byte MyFirstBit{
    	get {return this.data & 0x00000001;}
    	set { // kill existing bit in this place
    			this.data &= 0x11111110;
    			// set new bit
    			this.data |= value & 0x00000001;
    	}
    }
    Depending on your needs you also can use an indexer checking each bit you need using the index running from bit ti bit.
    Last edited by JonnyPoet; September 5th, 2008 at 10:40 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Oct 2007
    Posts
    22

    Re: Structure containing bit mask

    I'm simply reading a byte[] from a file. This file has a pre-defined layout. So for example..

    uint32 value1 (bytes 0 -3)
    uint16 value2 (4-5)
    byte value 3 (6)

    I can get these values fine but value2 could then be broken down further.

    value2 -> 1st 12bits are a numeric field and the remaining bits are single byte flags.

    As you say I could use setter to parse the value correctly but I wanted to make sure it wasn't possible to do someting like:

    uint32 value1
    BitArray value 2_1[12]
    BitArray value 2_2[1]
    BitArray value 2_3[1]
    BitArray value 2_4[1]
    BitArray value 2_5[1]
    byte value3

    I hope this explains exactly what I hope to do. Maybe it's not possible and byte is the smallest "unit" for StructLayout.

    Thanks.
    Last edited by sb101; September 5th, 2008 at 03:41 PM.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Structure containing bit mask

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    static class Program {
        static void Main() {
            byte[] bytes = { 0x02, 0x16, 0xaf, 0xff, 0xaa, 0x00, 0x01 };
            GCHandle bHand = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            Field f = (Field)Marshal.PtrToStructure(bHand.AddrOfPinnedObject(), typeof(Field));
            bHand.Free();
            // Here f will contain the values from the byte array
        }
    }
    
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
    public struct Field {
        [FieldOffset(0)]
        public uint one;
        [FieldOffset(sizeof(uint))]
        public ushort two;
        [FieldOffset(sizeof(uint) + sizeof(ushort))]
        public byte three;
    }

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Structure containing bit mask

    Quote Originally Posted by sb101
    ...
    I hope this explains exactly what I hope to do. Maybe it's not possible and byte is the smallest "unit" for StructLayout.
    IMHO Byte is the smallest unit, and bits are handled by using bitmasks as I showed in my example.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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

Featured