1

Can any explain what this section of code does: _num = (_num & ~(1L << 63));

I've have been reading up on RNGCryptoServiceProvider and came across http://codethinktank.blogspot.co.uk/2013/04/cryptographically-secure-pseudo-random.html with the code, I can follow most the code except for the section above.

I understand it ensuring that all numbers are positive, but I do not know how its doing that.

Full code

public static long GetInt64(bool allowNegativeValue = false)
{
    using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider())
    {
         byte[] _obj = new byte[8];
         _rng.GetBytes(_obj);
         long _num = BitConverter.ToInt64(_obj, 0);
         if (!allowNegativeValue)
         {
             _num = (_num & ~(1L << 63));
         }
         return _num;
     }
}

Any help explaining it would be appreciated

Tobias
  • 232
  • 1
  • 16
George Phillipson
  • 830
  • 11
  • 39
  • 1
    Have a look at this post (Explaining the tidle ~ operator) and this post that explains the bitwise shift (<<) operators. – Andre Lombaard Sep 06 '13 at 09:38
  • @user65439 That's better, however it's not just for my benefit. *Link only* answers are generally considered bad practice due to Link rot among other things. – DGibbs Sep 06 '13 at 09:43
  • Sorry the links went missing in my copy and paste above, see http://stackoverflow.com/questions/387424/what-is-the-tilde-in-an-enum-definition (Explaining the tidle ~ operator) and http://www.blackwasp.co.uk/CSharpShiftOperators.aspx that explains the bitwise shift (<<) operators. – Andre Lombaard Sep 06 '13 at 09:46
  • Hi user65439, thanks I have bookmarked those 2 pages. The blackwasp explains bitwise shift well. – George Phillipson Sep 06 '13 at 09:51

2 Answers2

4
a = ~(1L << 63) = ~0x1000000000000000 = 0x7fffffffffffffff

so m &= a clears the highest bit of m, thus ensuring it's positive, assuming the two's complement encoding of signed integers is used.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
4

<< is the bitshift operator 1L << 63 Results in shifting the 1 left 63 places or 1 followed by 63 0s

~ is i believe bitwise not, So this would apply to the above and result in 0 followed by 63 1s

& is bitwise and, it results in applying the and operation bitwise to both operands

Ultimately this appears to be filtering it down to 63 bits of data, since any higher bits will be zeroed out due to the and

The reason this works to force positive, is because typically the highest bit(#64 in your case) is used as a sign bit in most notations, and this code just essentially 0s it out, thus forcing it to be not negative, i.e positive

Karthik T
  • 31,456
  • 5
  • 68
  • 87