Explanation plz!

Can somebody explain clearly "line by line" what the code means?
Any good links given to read is also appreciated.

1
2
3
4
5
6
  register char c = getchar_unlocked();//register is used due to its repeated use
	while (!('0' <= c && c <= '9'))
 	{c = getchar_unlocked();}
 	while ('0' <= c && c <= '9')
	{n = (n<<3) + (n<<1) +  c - '0';//equivalent to n = n*10 + c - 48;
  	c = getchar_unlocked();}
You don't show the initial value of n.

Which parts do you regognize?
This is old-style code to convert a numerical string to an integer. IE, if given the string "242563", it will put the numerical value 242563 into your variable 'n'.

I do not recommend writing code like this, and if you are using this kind of code as an example or as something educational, I recommend you find something else instead.

Line by line explanation:


Line 1
I'm not 100% sure what getchar_unlocked() is, but I'm going to assume it pulls a single character from an input stream. IE: it will return the next character in the string you are analyzing.

The 'register' keyword here is largely ignored by modern compilers, and is not something you need to worry about. It exists as an optimization hint so the compiler will keep 'c' in a register rather than giving it a physical place in memory -- but modern compilers are smart enough to do that automatically when appropriate. So don't worry about it.

So really all this line does is create a new char variable 'c' and initialize it with the next character in the source string.

Lines 2,3

These are a loop that searches for the first numerical digit in the string. The while on line 2 will continue looping until 'c' contains a numerical digit between '0' and '9'. And the loop body on line 3 will keep fetching the next character in the source string.

Line 4

This will loop lines 5,6 until a non- numerical digit is found. IE: now that the previous loop has found the start of the numerical string, this loop will keep looping until we find its end.

Line 5

This line does exactly what the comment says it does, only in a stupid way. It uses bitshifting as an ill-advised "optimization" technique which may have been practical 20 years ago when multiplication was slow -- but nowadays writing code like this is shooting yourself in the foot and you are better off just multiplying normally.

n<<x is similar to doing n * pow(2,x).
So:
1
2
3
4
5
6
7
(n<<3) + (n<<1)
// is similar to
(n*pow(2,3)) + (n*pow(2,1))
// is similar to
(n*8) + (n*2)
// is similar to
(n*10)


The following bit... c - '0' takes the single numerical digit in c and converts it from an ASCII character code to a numerical integer. IE, the character '5' would actually become the number 5.

Combine that all together and this line is basically tacking the next digit of the number onto the right side of 'n'.
Example, if n=15 and c='4':
- multiply n by 10 to get 150
- add on numerical value of 4 to get 154

Line 6

Just gets the next character in the source string.
Thanks Disch! It was clear and detailed (:
Topic archived. No new replies allowed.