in typescript, as the number type can be int or float, so what if I use number type with bit operations
I have see some library use this
const a = 1.23
const b = a | 0
Sorry for the inaccurate expression, I'm just curious about this feature and not actually applying it. If I think about it from a memory perspective, like in C++, If I use
a |= 0
I can convert a into its binary form, Each bit is ORed with 0 and get the result. But what's the memory struct in Js, If a = 1.23, what's the binary form, and why the ORed will like a trunc operation?
and the result of b is 1, so can I use this method as a method of taking integers
I think I found the answer here: https://stackoverflow.com/a/52650645/28235582
number
type in TypeScript is the same as in JavaScript, which is effectively the same asdouble
in languages with such types. Please edit to fix this. • What do you mean "method of taking integers"? Yes,b
will always be some integer in the range of –2147483648 through 2147483647; like some kind of rounding and modulo. Is that really what you want? What should1e20
become?Math.trunc()
instead of| 0
unless you have a use case that compels otherwise.