I saw this method which is used for 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 I understand all of the part, what I'm not able to get is this:
bit shifting by odd number to
build the number n = (n<<3) + (n<<1) + (c-'0');
Why ignore the 3rd bit and how it's able to build it?
If anyone of you could explain me in simple way, it would be very much helpful.
Thanks!
n << i
isn * 2^i
. So,Basically, it means to shift the value of
n
one digit to the left.Adding it with
c + '0'
means adding the last digit with the integer value ofc
.