Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is C# BigInteger not always the same bit length?

I am trying to generate 1024bits random numbers with this code:

var bytes = RandomNumberGenerator.GetBytes(128);
var number = new BigInteger(bytes);

But when I inspect my number using BigInteger methods, its BitLength is always different.

For example, when running this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (int i = 0; i < 5; i++)
{
    var bi = new BigInteger(RandomNumberGenerator.GetBytes(128));
    Console.WriteLine($"{i}: {bi.GetBitLength()} bits | {bi.GetByteCount()} bytes");
}

I am getting this output:

0: 1021 bits | 128 bytes
1: 1023 bits | 128 bytes
2: 1022 bits | 128 bytes
3: 1021 bits | 128 bytes
4: 1022 bits | 128 bytes

What am I doing wrong / not understanding here ?

>Solution :

GetBitLength is documented as:

Gets the number of bits required for shortest two’s complement representation of the current instance without the sign bit.

And also, helpfully:

For positive integers the return value is equal to the ordinary binary representation string length.

Now, consider two (very small) BigInteger values – 1 and 3. It only takes 1 bit to represent the integer value 1, whereas it takes 2 bits to represent the integer 3.

Effectively, any leading 0 bits which might have been passed in the original byte array aren’t included in GetBitLength().

I suspect that’s absolutely fine for you. To put it another way: if you only wanted 4 bit numbers, would you want values between 0 and 15 inclusive, or between 8 and 15 inclusive? Because requiring GetBitLength() to return 4 would make you generate the latter…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading