Results 1 to 4 of 4

Thread: [SOLVED] Getting bit from Byte

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    1

    [SOLVED] Getting bit from Byte

    I am trying to assign a bit of a Byte to a control in my form. I have tried to search on internet but I couldn't find anything. I expect my code to be like this:

    Code:
    Public Class Form1
         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              Dim bytes As Byte() = {&B00, &B01, &B10, &B11}
              Dim bit As Boolean = MagicalFunctionThatGetsBitFromByte(bytes(0), 0) REM MagicalFunctionThatGetsBitFromByte(a As Byte, b As Integer) As Boolean | Where A is the byte and B is the bit (0 - 7)
              Button2.Enable = bit
         End Sub
    End Class
    Visual Basic .NET Framework 4.8
    Last edited by Mer08_; Jul 8th, 2022 at 11:28 AM.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: Getting bit from Byte

    That's over my head but I see something out there called a bit array that looks like you can do that:

    https://docs.microsoft.com/en-us/dot...N&view=net-6.0
    Please remember next time...elections matter!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Getting bit from Byte

    Two options come to mind immediately: a BitArray and bit-masking, e.g.
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim b As Byte = &B10110101
    5.  
    6.         For i = 0 To 7
    7.             Console.WriteLine(GetBit1(b, i))
    8.         Next
    9.  
    10.         Console.WriteLine()
    11.  
    12.         For i = 0 To 7
    13.             Console.WriteLine(GetBit2(b, i))
    14.         Next
    15.  
    16.         Console.ReadLine()
    17.     End Sub
    18.  
    19.     Private Function GetBit1(value As Byte, index As Integer) As Boolean
    20.         Dim bits = New BitArray({value})
    21.  
    22.         Return bits(index)
    23.     End Function
    24.  
    25.     Private Function GetBit2(value As Byte, index As Integer) As Boolean
    26.         Dim mask As Byte = 2 ^ index
    27.  
    28.         Return (value And mask) <> 0
    29.     End Function
    30.  
    31. End Module
    Whichever option you choose, you might write it as an extension method, e.g.
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module ByteExtensions
    4.  
    5.     <Extension>
    6.     Public Function GetBit(source As Byte, index As Integer) As Boolean
    7.         Dim mask As Byte = 2 ^ index
    8.  
    9.         Return (source And mask) <> 0
    10.     End Function
    11.  
    12. End Module
    and then you can call it on the value itself, e.g.
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim b As Byte = &B10110101
    5.  
    6.         For i = 0 To 7
    7.             Console.WriteLine(b.GetBit(i))
    8.         Next
    9.  
    10.         Console.ReadLine()
    11.     End Sub
    12.  
    13. End Module
    The BitArray would come into its own if you wanted to create it once and then use it multiple times.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Getting bit from Byte

    Quote Originally Posted by jmcilhinney View Post
    Code:
        Private Function GetBit2(value As Byte, index As Integer) As Boolean
            Dim mask As Byte = 2 ^ index
    
            Return (value And mask) <> 0
        End Function
    I'd go with this:-
    Code:
        Public Function GetBit(value As Byte, index As Integer) As Boolean
            Return CBool((value And (1 << index)))
        End Function
    The JIT produces cleaner, and by appearance, much faster assembly for this. I've observed that the JIT creates a lot of excessive x87 FPU instructions for exponent operations when this could be done using simple integer instructions which are faster.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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