© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
16 replies
Matt

Condense Byte[] to BitArray to smaller Byte[]

I have an array of 300 bytes, all either 0 or 1.
I need a function that takes in the byte array and performs a bitwise operation to convert the bytes to bits.
As an example:
If the first 8 bytes of the input array are
{ 01, 01, 00, 00, 00, 00, 00, 00 }
{ 01, 01, 00, 00, 00, 00, 00, 00 }

Then the BitArray should end up as
{ 00000011 }
{ 00000011 }

Which ends up as a byte 03
Obviously the array will have to be handled in chunks of 8. The resulting byte array should be about 40 bytes long, hence condensing the 300 byte array to a 40 bytes array.
I can't deviate from this structure unfortunately, I wish I could but this is what is required. Any advice on anything I should read up on or a fancy linq statement that will do this all for me would be greatly appreciated. Thank you
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Byte array to BitArray
C#CC# / help
2y ago
How to Read byte by byte until EOF?
C#CC# / help
2y ago
Get the bytes of byte[] as string
C#CC# / help
3y ago
Convert int[] to byte[]?
C#CC# / help
4y ago