Comments on: Double bitwise NOT (~~) https://j11y.io/cool-stuff/double-bitwise-not/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Art https://j11y.io/cool-stuff/double-bitwise-not/#comment-1845 Mon, 07 Feb 2011 19:08:39 +0000 https://j11y.io/?p=1433#comment-1845 I meant not going to work. (typo)

]]>
By: Art https://j11y.io/cool-stuff/double-bitwise-not/#comment-1844 Mon, 07 Feb 2011 19:08:01 +0000 https://j11y.io/?p=1433#comment-1844 ~~ is going to work as Math.floor for negative numbers.

~~(-2.999) = -2
Math.floor(-2.999) = -3

]]>
By: Mathias Bynens https://j11y.io/cool-stuff/double-bitwise-not/#comment-1843 Sun, 01 Aug 2010 09:09:43 +0000 https://j11y.io/?p=1433#comment-1843 Note that bitwise operators convert numbers to a 32-bit sequence before processing them, so the range is different.

Alternatives to Math.floor using bitwise operators will only work with positive signed 32-bit floats, i.e. numbers from 0 to +2,147,483,647 (2^31-1).

~~2147483647.1; // 2147483647
~~2147483648.1; // -2147483648

I’ve created a jsPerf test case to benchmark these different approaches. Feel free to add more possibilities!

]]>
By: Tom https://j11y.io/cool-stuff/double-bitwise-not/#comment-1842 Mon, 19 Apr 2010 12:29:23 +0000 https://j11y.io/?p=1433#comment-1842 Or you could add 0.5 and have Math.round() and by adding 1 you’d have Math.ceil();

Math.floor(x) == ~~(x)
Math.round(x) == ~~(x + 0.5)
Math.ceil(x) == ~~(x + 1)

Cheers,
Tom

]]>
By: Martin Kirk https://j11y.io/cool-stuff/double-bitwise-not/#comment-1841 Fri, 16 Apr 2010 08:39:41 +0000 https://j11y.io/?p=1433#comment-1841 @Carlos

ahhh — yes you are right !

]]>
By: Carlos https://j11y.io/cool-stuff/double-bitwise-not/#comment-1840 Fri, 16 Apr 2010 08:35:30 +0000 https://j11y.io/?p=1433#comment-1840 @Martin Kirk My question remains, shouldn’t this line

var SelectedOptions = Option3 & Option6 & Option10; //= 1000100100

be

var SelectedOptions = Option3 | Option6 | Option10; //= 1000100100

]]>
By: Martin Kirk https://j11y.io/cool-stuff/double-bitwise-not/#comment-1839 Fri, 16 Apr 2010 07:44:37 +0000 https://j11y.io/?p=1433#comment-1839 @Carlos

sorry… the HTML formatting has been fixed

]]>
By: Martin Kirk https://j11y.io/cool-stuff/double-bitwise-not/#comment-1838 Fri, 16 Apr 2010 07:36:38 +0000 https://j11y.io/?p=1433#comment-1838 @Carlos

what’s up with the & ???
the bitwise operator is ‘&’

]]>
By: SJL Web Design https://j11y.io/cool-stuff/double-bitwise-not/#comment-1837 Thu, 15 Apr 2010 19:12:01 +0000 https://j11y.io/?p=1433#comment-1837 Great article, thanks to Martin for his additional pieces of code too.

]]>
By: carlos https://j11y.io/cool-stuff/double-bitwise-not/#comment-1836 Thu, 25 Mar 2010 16:04:10 +0000 https://j11y.io/?p=1433#comment-1836 Shouldn’t @Martin Kirk example be:

var Option1 = 2
var Option2 = 4
var Option3 = 8
var Option4 = 16
var Option10 = 1024
 
var SelectedOptions = Option3 | Option6 | Option10; //= 1000100100
 
if(Option6 & SelectedOptions) {
    doSomething()
}
]]>