Solved

Convert integer into a bitarray?

Posted on 2004-10-04
6
705 Views
Last Modified: 2008-03-10
how can i turn say integer 129 into something like 10000001 that i can then loop through and tell whether it's a 1 or 0 at any point or on/off/whatever.  I can write all my own custom stuff if need be, but i didn't know if vb had something like this already?


like

dim test as integer = 129

dim test2 as arrayofsomesort

test2 = turntobits(test)

for a = 0 to test2.count - 1

if test2(a) = 1 then
'fire something here
end if

next

get where i'm doing?  real similiar to the concept of flags in Quake2 configs where to set certain options you add numbers and together and put taht number in your config.

Thanks,
Nick



0
Comment
Question by:NickUA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Accepted Solution

by:
gregasm earned 168 total points
ID: 12223194
Use static methods of the BitConverter class.

byte[] myBytes = BitConverter.GetBytes(myint);

Then you can use the byte array and go deeper into bits.
0
 
LVL 6

Assisted Solution

by:mogun
mogun earned 166 total points
ID: 12224159
Hi,

You don't have to loop through the bit array to check whether a bit is set or not.. Instead you can use bit operators to evaluate whether a bit is set or not....The following is code that uses && operator and << (left shift ) operators to evaluate whether a bit is set or not..

    Public Function IsBitSet(ByVal iConfig As Integer, ByVal iBitToCheck As Integer) As Boolean
        If (iConfig And (1 << (iBitToCheck - 1))) >= 1 Then
            Return True
        Else
            Return False
        End If
    End Function


iConfig is the integer number that you want to test...
iBitToCheck is the bit position that you want to check..

Let me tell you what the expression

iConfig And (1 << (iBitToCheck - 1))

mean...
Consider iConfig = 8 and iBitToCheck = 2

first take a look at (1<<(iBitToCheck -1 ))
This will left shift 1 by (iBitToCheck-1) times...
Hence this expression becomes
(1<<(2-1))
which becomes 1<<1, which mean left shift 1 by 1..
So 1 in binary format 00000001 becomes 00000010...
left shift makes the bits to move to the left side and adds a 0 on the right hand side..

now this will be "And" ed with 8 whose binary is 00001000
Hence
00001000 And
00000010 becomes
00000000 which is integer 0 ..
so the if fails and you get the result that bit 2 of 8 is not set..

Cheers
Mohan

Note: this code is not tested for all conditions..let me know if there are bugs..


0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 166 total points
ID: 12252795
BitVector32 class also.

Bob
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question