Last Updated: February 25, 2016
·
2.021K
· thiago

Bitwise trick for parsing float to int in JavaScript

Bitwise trick for parsing float point to int: use double negation ~~3.33 => 3

2 Responses
Add your response

What language does this work in specifically? I'm not aware of any that it's even valid syntax, let along a "bitwise" way to coerce floats.

Bitwise is implicitly not going to work unless you happen to know how your floats are encoded.

over 1 year ago ·

@richoh My bad, it works just with javascript. I thought I've tested it with other languages but I didn't.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside