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. Bitfield access to array

Bitfield access to array

Scheduled Pinned Locked Moved C#
data-structurestutorialquestiondiscussion
2 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.
  • L Offline
    L Offline
    LiamD
    wrote on last edited by
    #1

    I have an fixed length array :- Byte[] myArray = new Byte[50]; Now this represents a specific message structure. For example, the sort of thing I want to do is : 1. to read and set a bit field, Byte[25], bit 1 2. to read and set a 5 bit value, Byte[20], bits 0-5 3. to read and set a 32 bit number, Byte[10] These are just a few examples of what functions I want to perform on the array. But they highlight some of my challanges. Any thoughts on gneral ways to achieve this? Thanks, Liam

    D 1 Reply Last reply
    0
    • L LiamD

      I have an fixed length array :- Byte[] myArray = new Byte[50]; Now this represents a specific message structure. For example, the sort of thing I want to do is : 1. to read and set a bit field, Byte[25], bit 1 2. to read and set a 5 bit value, Byte[20], bits 0-5 3. to read and set a 32 bit number, Byte[10] These are just a few examples of what functions I want to perform on the array. But they highlight some of my challanges. Any thoughts on gneral ways to achieve this? Thanks, Liam

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      Well, to set a particular bit in a byte, I would use bitwise operators. The exclusive-or (^) would work:

      byte b = 35;
      b ^= 0x01;

      *Note - This will change the value of the bit. If it's 0, it will be changed to 1, if it's 1 it will be changed to zero. Of course, it's hard to tell what you mean by bit 1. But to set a particular bit in the byte array is not much different:

      byte[] b = new byte[50];
      ...
      b[25] ^= 0x01;

      To read the value, you can use a bitwise-and ( & ) :

      if (b[20] & 0x1f == a) { ... }

      I'm not sure what you mean by setting a 32 bit number on a single byte though.


      Logifusion[^]

      Last modified: Friday, July 28, 2006 9:43:29 AM --

      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