Click here to Skip to main content
15,566,522 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all.

I'm having a little trouble. I need to read bits from a byte, the idea is that I read a byte from a stream and the I have to read bits from that byte.
A simple example would be I read a byte "A" which would be byte "065". Now I need to read that as "01000001", well actually I need to read it as "0","1","0","0","0","0","0","1" or any other interpretation like True or False it's irrelevant.

Hypothetically, the code would count how many "1"(Trues) are there in a file.

I can live with any encoding as long as I can read any byte in the data as the data I need to read is God only knows what.

I've tried a couple of things but none of them worked as it should.
I just can't get to the bottom of it. I read bytes no problem but just can't seem to get the bits part done.

I'd appreciate any code sample. Thanks.
Updated 17-Oct-21 15:59pm

Have a read of this article on BitWise operations to determine the state of individual bits. That was the original way of doing things.[^]

We now also have BitArray available e.g.;
Dim x As Byte
x = &H65
Dim ba As New BitArray({x})
MsgBox("Bit Status; " + ba.Get(7).ToString + _
                    " " + ba.Get(6).ToString + _
                    " " + ba.Get(5).ToString + _
                    " " + ba.Get(4).ToString + _
                    " " + ba.Get(3).ToString + _
                    " " + ba.Get(2).ToString + _
                    " " + ba.Get(1).ToString + _
                    " " + ba.Get(0).ToString)
Share this answer
Holc 10-Oct-10 11:31am    
thanks this will do just fine, i know about the bitarray but it just didn't seem to work in whichever way i'd do it. thanks again.
DaveAuld 12-Oct-10 7:21am    
Your welcome, just remember, that Bit 7 is Left MSB, and Bit 0 is Right LSB.
If all you want is counting the number of bits set in a large array of bytes, there is no need to perform bit operations at all: all it takes is a magic array that converts all possible byte values to the number of bits set in that byte value.

int[] bitsSet=new int[256] {0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4 ...};

There are many ways to come up with the correct values for this array:
1. simply counting bits using bit operations;
2. constructing the array dynamically, e.g. using 8 nested for loops, each representing one bit being clear or set;
3. detecting a repetition in the number pattern;
4. using recursion.

Whatever you choose, once the bitsSet array is available, you'll get way more performance out of it, as you are taking care of 8 bits at a time by using a simple array lookup and one addition.

Share this answer
Richard Deeming 18-Oct-21 5:37am    
Hopefully the OP managed to solve their question in the last eleven years. :)

It was dragged back into the "active" list by the repost of solution 1 posted as solution 2.
Luc Pattyn 18-Oct-21 11:14am    
Quick Answers it is :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900