I saw the double tilde (~~) in the code and wondered what it was. Of course, single tilde is the bitwise not operator. Googling finds that double tilde is a faster Math.floor(). http://stackoverflow.com/questions/5971645/what-is-the-doubl...
JS1k demo: “Highway at Night”
91–100 of 100 posts
Re: JS1k demo: “Highway at Night”
#92Earlier quoted context omitted.
> if(~someStringOrArray.indexOf(searchTerm)) Do you think the person maintaining your code 2-5 years from now will understand that easily and quickly? For maintenance, it's important to be as clear as possible, not as tricky as possible.
Which is why I only use it on solo projects. That being said, ~indexOf is hardly tricky. if(string.indexOf(term)) is far trickier, because if you didn't realize it returning 0 was a positive result, you easily could write it without a -1 check and not notice it during testing if no values had an index of 0. Then months later you would have a hell of a time debugging it because it would look so innocuous. On the other…
The premises was that the future dev won't know. It was hard to write, it should be hard to read moderately applies, I guess.
Re: JS1k demo: “Highway at Night”
#93Earlier quoted context omitted.
Learning about bitwise operators (and everything they entail - so binary in general, endianness, etc) was without a doubt the most beneficial thing I've ever done for my programming abilities. It was a complete eye opener to how computers work on a far more fundamental level than any other aspect of programming had previously revealed to me. For anyone without a traditional CS educational background, if you don't kno…
You definitely should learn bitwise operators, but if other people have to work with your code I'm not generally in favor of applying them in clever ways where there's a slightly more verbose but much more readable solution (e.x. "~x.indexOf(y)" vs "x.indexOf(y) !== 0"... just type the extra 3 characters)
In any case, it's all a matter of style... because if you don't know that indexOf will return 0 for a positive match, you will still mess up.
Re: JS1k demo: “Highway at Night”
#94Earlier quoted context omitted.
I doubt that (all) humans think in javascript :P Im sure the brain has its tricks, but they dont need to have something in common with the ones used here. I wonder what Kolmogorov Complexity this program has. Obviously <= 1023.
How does Kolmogorov Complexity deal with the fact that you're specifying that number within a certain execution context (in this case JavaScript)? Otherwise you could simply reduce the program to a single bit and have an interpreter that includes the original program as a primitive. This program without the libraries that it accesses would be a lot larger than it is. Also, shouldn't the number be expressed in bits ra…
Re: JS1k demo: “Highway at Night”
#95Earlier quoted context omitted.
It's a bit more than that. It can also be used to convert strings (containing numeric values) to Numbers. More commonly seen than the prefix "~~" is "|0", because of it's low order precedence. > '15' '15' > ~~'15' 15 > '15'|0 15 > 15.99|0 15 > ~~15 * 1.3 19.5 > 15 * 1.3 | 0 19
Everyone I know uses the + operator, I rarely see others. Funny that I'm not seeing + here.
Part of this is because bitwise operations will convert whatever the input value is to a 32-bit integer value before performing the operation, and not fail (invalid input becomes 0 opposed to NaN).
Re: JS1k demo: “Highway at Night”
#96Earlier quoted context omitted.
Learning about bitwise operators (and everything they entail - so binary in general, endianness, etc) was without a doubt the most beneficial thing I've ever done for my programming abilities. It was a complete eye opener to how computers work on a far more fundamental level than any other aspect of programming had previously revealed to me. For anyone without a traditional CS educational background, if you don't kno…
> if(~someStringOrArray.indexOf(searchTerm)) Do you think the person maintaining your code 2-5 years from now will understand that easily and quickly? For maintenance, it's important to be as clear as possible, not as tricky as possible.
Re: JS1k demo: “Highway at Night”
#97This is so very impressive. I like how it uses canvas to make the moon: // Moon c.fillText("(",99,-99);
This makes you realise how old games must have been driven by the technology.
Re: JS1k demo: “Highway at Night”
#98Re: JS1k demo: “Highway at Night”
#99I saw the double tilde (~~) in the code and wondered what it was. Of course, single tilde is the bitwise not operator. Googling finds that double tilde is a faster Math.floor(). http://stackoverflow.com/questions/5971645/what-is-the-doubl...
Learning about bitwise operators (and everything they entail - so binary in general, endianness, etc) was without a doubt the most beneficial thing I've ever done for my programming abilities. It was a complete eye opener to how computers work on a far more fundamental level than any other aspect of programming had previously revealed to me. For anyone without a traditional CS educational background, if you don't kno…
var exists = ~someStringOrArray.indexOf(searchTerm);
if(exists){...}
or // If exists do...
if(~someStringOrArray.indexOf(searchTerm))Re: JS1k demo: “Highway at Night”
#100I saw the double tilde (~~) in the code and wondered what it was. Of course, single tilde is the bitwise not operator. Googling finds that double tilde is a faster Math.floor(). http://stackoverflow.com/questions/5971645/what-is-the-doubl...
I wouldn't recommend anyone doing optimizations in especially JavaScript unless it's really needed. Remember that optimizations are the root of all evil!