Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
To calculate the most significant digit of any number log can be directly used to calculate it.
Suppose the number is N then Let double K = Math.log10(N); now K = K - Math.floor(K); int X = (int)Math.pow(10, K); X will be the most significant digit.
This method is fast and doesn’t require the use of 3rd variable.
Multiplying by 2 means shifting all the bits to left and dividing by 2 means shifting to the right.
Example : 2 (Binary 10): shifting left 4 (Binary 100) and right 1 (Binary 1)
Although this trick is not much better than using % operator but is sometimes efficient (with large numbers). Use & operator:
num = 5
Binary: “101 & 1” will be 001, so true
num = 4
Binary: “100 & 1” will be 000, so false.