VB.NET - Convert UShort into boolean array

414 Views Asked by At

I'm updating some old code written by another programmer, and i need to do some changes. I have an unsigned integer stored into Short var and need to get its binary code in an array of bool.

How can i do this?

2

There are 2 best solutions below

0
On

try this perhaps

    Dim MyShort As uShort = 32
    Dim oByteArray As New System.Collections.BitArray(System.BitConverter.GetBytes(MyShort))
0
On

In addition to Trevor's answer, you can use a BitVector32 as well:

Dim myValue As UShort = 32
Dim bVector As New BitVector32(CInt(myValue))

BitVector32 is slightly more efficient than BitArray but is limited to 32 bits. BitVector32 allows you to create sections in the bits for different purposes and treat them as flags.