Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bit-sized integers in C#

Bit-sized integers in C#

Scheduled Pinned Locked Moved C#
csharpc++question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S O S
    wrote on last edited by
    #1

    I have a byte[] that contains what in C++ would be this struct: struct { unsigned type : 5; unsigned level : 2; unsigned bouncing : 1; unsigned emp : 1; unsigned isBomb : 1; unsigned shrapCount : 5; unsigned fireType : 1; }; Is there a way to make the byte[] into a class in C# without doing each member seperately with bitwise AND & shifting?

    S 1 Reply Last reply
    0
    • S S O S

      I have a byte[] that contains what in C++ would be this struct: struct { unsigned type : 5; unsigned level : 2; unsigned bouncing : 1; unsigned emp : 1; unsigned isBomb : 1; unsigned shrapCount : 5; unsigned fireType : 1; }; Is there a way to make the byte[] into a class in C# without doing each member seperately with bitwise AND & shifting?

      S Offline
      S Offline
      Scott Danley
      wrote on last edited by
      #2

      You might find the BitVector32 class useful to base your code off of. You would create references to your types with the following code: BitVector32 bv = new BitVector32(0); BitVector32.Section type = BitVector32.CreateSection(31); BitVector32.Section level = BitVector32.CreateSection(3,type); BitVector32.Section bouncing = BitVector32.CreateSection(1, level); BitVector32.Section emp = BitVector32.CreateSection(1, bouncing); BitVector32.Section isBomb = BitVector32.CreateSection(1, emp); BitVector32.Section shrapCount = BitVector32.CreateSection(31, isBomb); BitVector32.Section fireType = BitVector32.CreateSection(1, shrapCount); The values are accessed as follows: bv[type] = typeValue; bv[bouncing] = 1; Access to your values with no &, |, or <<

      S 1 Reply Last reply
      0
      • S Scott Danley

        You might find the BitVector32 class useful to base your code off of. You would create references to your types with the following code: BitVector32 bv = new BitVector32(0); BitVector32.Section type = BitVector32.CreateSection(31); BitVector32.Section level = BitVector32.CreateSection(3,type); BitVector32.Section bouncing = BitVector32.CreateSection(1, level); BitVector32.Section emp = BitVector32.CreateSection(1, bouncing); BitVector32.Section isBomb = BitVector32.CreateSection(1, emp); BitVector32.Section shrapCount = BitVector32.CreateSection(31, isBomb); BitVector32.Section fireType = BitVector32.CreateSection(1, shrapCount); The values are accessed as follows: bv[type] = typeValue; bv[bouncing] = 1; Access to your values with no &, |, or <<

        S Offline
        S Offline
        S O S
        wrote on last edited by
        #3

        Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups