java - How this bitshift to build the number works? -
i saw method used faster reading of positive value of long.
public static long readlong(inputstream in) throws ioexception { long n = 0; int c = in.read(); while (c < 48 || c > 57) { c = in.read(); } while (c >= 48 && c <= 57) { n = (n<<3) + (n<<1) + (c-'0'); c = in.read(); } return n; }
while understand of part, i'm not able this:
bit shifting odd number build number n = (n<<3) + (n<<1) + (c-'0');
why ignore 3rd bit , how it's able build it?
if of explain me in simple way, helpful.
thanks!
n << i
n * 2^i
. so,
(n<<3) + (n<<1) = (n * 2^3) + (n * 2^1) = n * (2^3 + 2^1) = n * 10
basically, means shift value of n
1 digit left.
adding c + '0'
means adding last digit integer value of c
.
Comments
Post a Comment