1

I've never wandered into reading binary data before. I'm trying to learn now, and make a simple application to read the header data from a FLAC file and display the information in human readable format.

As a first, given that the first bit of data I'm interested in is 0x10000100, how do I use C# to read the first bit (1) and then read the int value stored in the subsequent 7 bits? I know already how to read the byte into a byte array using binaryreader... Just don't know how to parse this data in code.

Thanks in advance.

Mirrana
  • 1,601
  • 6
  • 28
  • 66

1 Answers1

3

Binray "and" &, "or" |, "not" ~ are used to extract bits.

Here is approximate code:

byte value = 0x84;
bool flag = (value & 0x80) != 0;
var intPart = value & ~0x80;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Cool, thanks. Is there any source you can recommend to explain a bit in detail how all this works, and why that notations is why it is? I sorta get it but I feel sort of lacking still. – Mirrana May 24 '12 at 17:11
  • search terms - "C# bit manipulation" or "C# bitwise operations". This question http://stackoverflow.com/questions/93744/most-common-c-sharp-bitwise-operations looks like it has plenty of info. Note that the topic has common syntax for many languages (C/Java/C#/JavaScript...) so not necessary to limit search by language. – Alexei Levenkov May 24 '12 at 17:15