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. Visual Basic
  4. How to convert a BitArray of length 8 into a single byte

How to convert a BitArray of length 8 into a single byte

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
3 Posts 3 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.
  • E Offline
    E Offline
    edmonson
    wrote on last edited by
    #1

    We're working with a byte variable and i am using it like a bitfield. To do this, i use BitArray class to avoid logical operations like and, or,... My problem is when i want to reverse the operation. Starting from my BitArray object of size 8, is possible to convert to a single byte ? Thanks.

    G D 2 Replies Last reply
    0
    • E edmonson

      We're working with a byte variable and i am using it like a bitfield. To do this, i use BitArray class to avoid logical operations like and, or,... My problem is when i want to reverse the operation. Starting from my BitArray object of size 8, is possible to convert to a single byte ? Thanks.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I think that you can use the CopyTo method to copy the array into a byte array with a single element.

      --- single minded; short sighted; long gone;

      1 Reply Last reply
      0
      • E edmonson

        We're working with a byte variable and i am using it like a bitfield. To do this, i use BitArray class to avoid logical operations like and, or,... My problem is when i want to reverse the operation. Starting from my BitArray object of size 8, is possible to convert to a single byte ? Thanks.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        The BitArray class doesn't support conversions to other types (EDIT: I completely forgot about CopyTo!!). If you're only using 8 bits, you're much better off just using a Byte and doing the bit manipulation yourself. It's easy to write your own methods to automate some of this, like setting or resetting a bit. A quick little whip up:

        Public Shared Function SetBit(ByVal value As Byte, ByVal position As Integer) As Byte
        If position < 1 Or position > 8 Then
        Throw New ArgumentOutOfRangeException("Position must be between 1 and 8.")
        End If

        Dim mask As Byte = CByte(2 ^ (position - 1))
        If Not value And mask Then
            value = value Or mask
        End If
        Return value
        

        End Function

        -- modified at 13:06 Wednesday 2nd May, 2007

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        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