Thursday, March 27, 2008

Got a sudden insight now :-)

x=[11,22,33,44]

To get last element, we use x[-1]

Instead of using the negative index directly, we can use the bitwise NOT operator ~, since ~0=-1 and ~1=-2 etc!

So you can simply say x[~0] to access the last element, x[~1] for last but one etc.

[Hacker News discussion]

No comments: