Why does this random value have a 25/75 distribution instead of 50/50?

Because nextDouble works like this: (source) public double nextDouble() { return (((long) next(26) << 27) + next(27)) / (double) (1L << 53); } next(x) makes x random bits. Now why does this matter? Because about half the numbers generated by the first part (before the division) are less than 1L << 52, and therefore their … Read more

Position of least significant bit that is set

Bit Twiddling Hacks offers an excellent collection of, er, bit twiddling hacks, with performance/optimisation discussion attached. My favourite solution for your problem (from that site) is «multiply and lookup»: unsigned int v; // find the number of trailing zeros in 32-bit v int r; // result goes here static const int MultiplyDeBruijnBitPosition[32] = { 0, … Read more

What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?

GCC has: — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in X, starting at the most significant bit position. If X is 0, the result is undefined. — Built-in Function: int __builtin_clzl (unsigned long) Similar to `__builtin_clz’, except the argument type is `unsigned long’. — Built-in Function: int __builtin_clzll … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)