334

I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?

Sam
  • 35,545
  • 30
  • 157
  • 202
hd.
  • 15,498
  • 41
  • 106
  • 159

19 Answers19

536

Use the unary + operator, which converts its operand into a number.

+ true; // 1
+ false; // 0

Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says.

lonesomeday
  • 215,182
  • 48
  • 300
  • 305
  • 58
    Albeit cool (I had never thought of this), it is [**incredibly** slow](http://jsperf.com/boolean-int-conversion/2) (97% slower in Chrome, to be exact). Be wary! – Qix - MONICA WAS MISTREATED Mar 17 '14 at 04:53
  • @Qix Thank you: that is fascinating. Actually, no, it's astonishing. Though a comparison with the Number constructor would also be interesting... – lonesomeday Mar 17 '14 at 08:59
  • 5
    Check out [this revision](http://jsperf.com/boolean-int-conversion/3). `Number()` is even slower. – Qix - MONICA WAS MISTREATED Mar 17 '14 at 16:11
  • 28
    It appears `bool === true ? 1 : 0` is the fastest, with a close second from `bool | 0`. – Qix - MONICA WAS MISTREATED Mar 17 '14 at 16:12
  • 1
    Multiplying (e.g. 3*false) feels so wrong, but it works. :) Thanks! – mattsoave May 08 '15 at 16:24
  • Strange, faster method here in my chrome... tested with perfomance.now() in this function `function timeit(a, b, c, d) {c = performance.now(); for(d = 0; d < b; d++) a(); return performance.now()-c}`. Pass function and number of loops. – caiohamamura Apr 30 '16 at 01:22
  • 1
    @Qix When you [do the microbenchmarks properly](https://mrale.ph/blog/2012/12/15/microbenchmarks-fairy-tale.html), all of the test cases [have the same performance](https://jsperf.com/boolean-int-conversion/13). – Bergi Jul 21 '19 at 21:13
  • damn, I didn't realize how slow this was – rpivovar Oct 28 '19 at 01:48
  • With TypeScript linter tslint installed in your IDE, this gives an error `Operator '+' cannot be applied to types 'number' and 'boolean'`. So, you shouldn't do this in JavaScript as well. – Derk Jan Speelman Dec 02 '19 at 10:57
  • @lonesomeday mine surely does, maybe it's my relatively strict tslint config – Derk Jan Speelman Dec 09 '19 at 14:59
  • 2
    @DerkJanSpeelman The fact that something is not allowed in Typescript does not mean that you shouldn't do it in Javascript. They are different (albeit related) languages. – lonesomeday Dec 09 '19 at 17:14
  • The ternary solution is one of the fastest ways. Other solutions like `+true` or `Number(true)` are extremely slow. See [benchmark](https://jsben.ch/dYdis). – Dominik Jan 30 '21 at 11:47
410

Javascript has a ternary operator you could use:

var i = result ? 1 : 0;
Andy Rose
  • 15,632
  • 7
  • 38
  • 48
  • 10
    Best answer. Why? This works on truthiness which is more general and accepts any type (string, number, etcetera.) The unary answer is clever indeed, but if I pass it a string it returns `NaN`. So if you want L33T and guarantee the input, go urary, otherwise methinks the ternary + truthy test is best. – gdibble Jun 13 '17 at 18:31
  • This solution is basically minimizing an `if` statement using the ternary operator. – Mr PizzaGuy Nov 11 '20 at 19:34
  • 1
    The ternary solution is one of the fastest ways. Other solutions like `+true` or `Number(true)` are extremely slow. See [benchmark](https://jsben.ch/dYdis). – Dominik Jan 30 '21 at 11:46
131

Imho the best solution is:

fooBar | 0

This is used in asm.js to force integer type.

kralyk
  • 3,703
  • 1
  • 26
  • 30
77

I prefer to use the Number function. It takes an object and converts it to a number.

Example:

var myFalseBool = false;
var myTrueBool = true;

var myFalseInt = Number(myFalseBool);
console.log(myFalseInt === 0);

var myTrueInt = Number(myTrueBool);
console.log(myTrueInt === 1);

You can test it in a jsFiddle.

Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278
René
  • 9,346
  • 4
  • 39
  • 49
  • 5
    This is the best answer by far. At the bottom of course. Only "it takes an object" isn't right. – Rudie Oct 20 '13 at 02:49
  • 2
    Link to mdn is much better than w3schools(eeek !): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number – Olivvv Jan 10 '14 at 17:17
  • 3
    I think this is the best way because it's easy to read and intention-revealing. – Sam Jan 23 '14 at 04:14
  • 4
    It is also *the* slowest. – Qix - MONICA WAS MISTREATED Mar 18 '14 at 16:08
  • Nice, that's exactly what I would do. Instead of using a ternary expression. – kalicki2k Oct 23 '20 at 12:43
  • The ternary solution is one of the fastest ways. Other solutions like `+true` or `Number(true)` are extremely slow. See [benchmark](https://jsben.ch/dYdis). – Dominik Jan 30 '21 at 11:49
  • There is a gotcha. If the number is greater than 32 bits, the `| 0` operator will yield a negative value. I personally solved this by using `||` instead of `|` – Ploppz Apr 01 '21 at 14:34
48

I created a JSperf comparison of all suggested answers.

TL;DR - the best option for all current browsers is:

val | 0;

.

Update:

It seems like these days they are all pretty identical, except that the Number() function is the slowest, while the best being val === true ? 1 : 0;.

Gal Talmor
  • 886
  • 8
  • 14
46

The typed way to do this would be:

Number(true) // 1
Number(false) // 0
Philip
  • 19,974
  • 2
  • 28
  • 31
33

I just came across this shortcut today.

~~(true)

~~(false)

People much smarter than I can explain:

http://james.padolsey.com/javascript/double-bitwise-not/

tonyjcamp
  • 339
  • 3
  • 3
17

When JavaScript is expecting a number value but receives a boolean instead it converts that boolean into a number: true and false convert into 1 and 0 respectively. So you can take advantage of this;

var t = true;
var f = false;

console.log(t*1); // t*1 === 1
console.log(f*1); // f*1 === 0 

console.log(+t); // 0+t === 1 or shortened to +t === 1
console.log(+f); //0+f === 0 or shortened to +f === 0

Further reading Type Conversions Chapter 3.8 of The Definitive Guide to Javascript.

Gordon
  • 18,882
  • 4
  • 33
  • 72
Charlie Lynch
  • 351
  • 3
  • 6
13

I was just dealing with this issue in some code I was writing. My solution was to use a bitwise and.

var j = bool & 1;

A quicker way to deal with a constant problem would be to create a function. It's more readable by other people, better for understanding at the maintenance stage, and gets rid of the potential for writing something wrong.

function toInt( val ) {
    return val & 1;
}

var j = toInt(bool);

Edit - September 10th, 2014

No conversion using a ternary operator with the identical to operator is faster in Chrome for some reason. Makes no sense as to why it's faster, but I suppose it's some sort of low level optimization that makes sense somewhere along the way.

var j = boolValue === true ? 1 : 0;

Test for yourself: http://jsperf.com/boolean-int-conversion/2

In FireFox and Internet Explorer, using the version I posted is faster generally.

Edit - July 14th, 2017

Okay, I'm not going to tell you which one you should or shouldn't use. Every freaking browser has been going up and down in how fast they can do the operation with each method. Chrome at one point actually had the bitwise & version doing better than the others, but then it suddenly was much worse. I don't know what they're doing, so I'm just going to leave it at who cares. There's rarely any reason to care about how fast an operation like this is done. Even on mobile it's a nothing operation.

Also, here's a newer method for adding a 'toInt' prototype that cannot be overwritten.

Object.defineProperty(Boolean.prototype, "toInt", { value: function()
{
    return this & 1;
}});
  • I've had two downvotes for this post. Why don't you explain why you downvoted it. Otherwise it's just a downvote without justification. – Nicholas R. Grant Sep 18 '17 at 03:01
  • 1
    99 times the results of jsperf just lead you up the premature optimisation path, optimising nanoseconds off a loop when you should be focussed on that ugly SQL statement instead. thanks for providing a few different ways to approach this – RozzA Oct 09 '17 at 21:25
  • What SQL statement? There's not a single query here. If you're referring to JSPerf, I was linking that from someone else's test. It's not my own. I honestly don't care about the performance aspect of this since it's a nothing operation. I created my own language that was nearly functionality identical to JS and I remember that casting to int was a stupidly fast operation. Climbing prototype chains was not. Which is why I'd still recommend the first way I did it, with a simple function that can be inlined by the compiler. – Nicholas R. Grant Oct 10 '17 at 16:52
  • the SQL thing 'twas a generalisation. thanks for the insight – RozzA Oct 12 '17 at 04:14
13

The unary + operator will take care of this:

var test = true;
// +test === 1
test = false;
// +test === 0

You'll naturally want to sanity-check this on the server before storing it, so that might be a more sensible place to do this anyway, though.

xanatos
  • 102,557
  • 10
  • 176
  • 249
Gustav Barkefors
  • 4,626
  • 24
  • 30
  • I have changed the comments to `===`, because `true == 1` is true even withou the "explicit conversion :-) `true === 1` instead is false. – xanatos Oct 19 '11 at 11:51
11

You can also add 0, use shift operators or xor:

val + 0;
val ^ 0;
val >> 0;
val >>> 0;
val << 0;

These have similar speeds as those from the others answers.

REMqb
  • 353
  • 2
  • 7
9

In my context, React Native where I am getting opacity value from boolean, the easiest way: Use unary + operator.

+ true; // 1
+ false; // 0

This converts the boolean into number;

style={ opacity: +!isFirstStep() }
Jose Velasco
  • 124
  • 2
  • 6
6

+!! allows you to apply this on a variable even when it's undefined:

+!!undefined    // 0
+!!false        // 0
+!!true         // 1

+!!(<boolean expression>)  // 1 if it evaluates to true, 0 otherwise
silkfire
  • 20,433
  • 12
  • 70
  • 93
4

You could do this by simply extending the boolean prototype

Boolean.prototype.intval = function(){return ~~this}

It is not too easy to understand what is going on there so an alternate version would be

Boolean.prototype.intval = function(){return (this == true)?1:0}

having done which you can do stuff like

document.write(true.intval());

When I use booleans to store conditions I often convert them to bitfields in which case I end up using an extended version of the prototype function

Boolean.prototype.intval = function(places)
{
 places = ('undefined' == typeof(places))?0:places; 
 return (~~this) << places
}

with which you can do

document.write(true.intval(2))

which produces 4 as its output.

DroidOS
  • 7,220
  • 11
  • 70
  • 142
4
let integerVariable = booleanVariable * 1;
Esger
  • 981
  • 10
  • 17
4

TL;DR: Avoid Number constructor, unary +; use a simple if all the time; resort to bool | 0 or 1 * bool if benchmarks in your project do better this way.

This is quite an old question, and there exist many valid answers. Something I've noticed is that all benchmarks here are irrelevant - none take into account branch prediction. Also, nowadays, JS engines don't simply interpret the code, they JIT compile it to native machine code and optimize it prior to execution. This means that, besides branch prediction, the compiler can even substitute expressions with their final value.

Now, how do these 2 factors affect the performance of, well, boolean to integer conversion? Let's find out! Before we get into the benchmarks, it is important to know what we benchmark. For the conversion, we're using the following seven conversion methods:

  • Number constructor: Number(bool)
  • If statement (ternary used): bool ? 1 : 0
  • Unary operator +: +bool
  • Bitwise OR: bool | 0
  • Bitwise AND: bool & 1
  • Bitwise double NOT: ~~bool
  • Number multiplication: bool * 1

"Conversion" means converting false to 0 and true to 11. Each conversion method is ran 100000 times, measuring operations/millisecond. In the following tables, conversion methods will be grouped to their results accordingly. The results are from my machine, which features an AMD Ryzen 7 4800HS as its CPU.

The first benchmark converts the constant true:

Method Edge/Chromium (V8) Firefox (Spidermonkey)
Number(bool) 83103 1088
bool ? 1 : 0 83073 7732
+bool 83372 1043
bool | 0 83479 9344
bool & 1 83242 9354
~~bool 83293 9316
bool * 1 83504 9316

Interesting! V8 shows some huge numbers, all of them approximately the same! Spidermonkey doesn't really shine, but we can see that the bitwise and multiplication tricks come first, and the ternary if second. What are the takeaways? Chrome browsers manage to replace our conversions with simply the value 1. This optimization will take place where we can mentally replace the boolean to a constant value.

That above isn't a situation we'll ever encounter in real projects. So let's change our variables: the bool is now Math.random() < 0.5. This yields a 50% chance of true, 50% of false. Do our results change? Let's run this benchmark to see.

Method Edge/Chromium (V8) Firefox (Spidermonkey)
Number(bool) 2405 662
bool ? 1 : 0 1482 1580
+bool 2386 673
bool | 0 2391 2499
bool & 1 2409 2513
~~bool 2341 2493
bool * 1 2398 2518

The results are more consistent now. We see similar numbers for ternary if, bitwise, and multiplication methods, but the Number constructor and unary + perform better on V8. We can presume from the numbers that V8 replaces them with whatever instructions it's using for the bitwise tricks, but in Spidermonkey those functions do all the work.

We haven't still tackled one factor we mentioned above: branch prediction. Let's change, in this benchmark, our boolean variable to Math.random() < 0.01, which means 1% true, 99% false.

Method Edge/Chromium (V8) Firefox (Spidermonkey)
Number(bool) 2364 865
bool ? 1 : 0 2352 2390
+bool 2447 777
bool | 0 2421 2513
bool & 1 2400 2509
~~bool 2446 2501
bool * 1 2421 2497

Unexpected? Expected? I'd say the latter, because in this case branch prediction was successful in almost all cases, given the tiny difference between the ternary if and bitwise hacks. All other results are the same, not much else to say here.

This endeavour brings us back to the original question: how to convert bool to int in Javascript? Here are my suggestions:

  • Avoid Number(bool) and +bool. These 2 methods do a lot of work under the hood, and even though Chrome managed to optimize them in our benchmarks, Firefox did not, and there might be some situations where these optimizations won't be done by the compiler. Besides that, not everyone's on Chrome! I still have to put up with that, don't you?...
  • Use if statements, in general. Don't get smart - the browser will do better, usually, and usually means most of the situations. They are the most readable and clear out of all the methods here. While we're at readability, maybe use if (bool) instead of that ugly ternary! I wish Javascript had what Rust or Python have...
  • Use the rest when it's truly necessary. Maybe benchmarks in your project perform sub-standard, and you found that a nasty if causes bad performance - if that's the case, feel free to get into branchless programming! But don't go too deep in that rabbit hole, nobody will benefit from things like -1 * (a < b) + 1 * (a > b), believe me.

I will be forever grateful to you for reading until the end - this is my first longer, significant StackOverflow answer and it means the world to me if it's been helpful and insightful. If you find any errors, feel free to correct me!


  1. Defined the conversion because it's not truly clear what boolean to integer means. For example, Go does not support this conversion at all.
Teodor Maxim
  • 85
  • 2
  • 6
  • 1
    upvoted for the effort but I can't think of a real use case when performance of such operation would matter :) – skwisgaar Feb 09 '21 at 10:15
  • @skwisgaar Completely agree, although I think this operation is mostly thought of using when people feel the need to optimize – Teodor Maxim Feb 14 '21 at 17:28
  • 1
    Excellent post, but am trying to figure out the interpretation of the benchmark numbers, as you indicate 'operations/second'. In the V8 browser debugger of my 5yr old Acer laptop, the following million operations run in 36ms(!): `start=performance.now(); for (let i = 0; i < 1000000; i++) {let x = +(Math.random()<0.5);} end=performance.now(); console.log(end-start)`. What am I misinterpreting? – Trentium Feb 17 '21 at 22:48
  • Thanks for your reply! Running the benchmarks myself, the numbers look more like operations/_millisecond_. Although in both Chrome's and Firefox's DevTools the same benchmarks seem to run faster on my machine too - maybe JSBench is a bottleneck. I'll change the measure unit for now, but this must be looked further into. – Teodor Maxim Feb 18 '21 at 08:01
  • @TeodorMaxim just saw that you had replied... Sounds like it's not clear what JSBench is reporting... You might be best to create your own mini benchmark tool, as at least you'll know exactly what's being measured in addition knowing exactly how to interpret the results. – Trentium Feb 26 '21 at 17:34
2

try

val*1

let t=true;
let f=false;

console.log(t*1);
console.log(f*1)
Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
1

I have tested all of this examples, I did a benchmark, and finally I recommend you choose the shorter one, it doesn't affect in performance.

Runned in Ubuntu server 14.04, nodejs v8.12.0 - 26/10/18

    let i = 0;
console.time("TRUE test1")
    i=0;
    for(;i<100000000;i=i+1){
        true ? 1 : 0;
    }
console.timeEnd("TRUE test1")


console.time("FALSE test2")
    i=0;
    for(;i<100000000;i=i+1){
        false ? 1 : 0;
    }
console.timeEnd("FALSE test2")

console.log("----------------------------")

console.time("TRUE test1.1")
    i=0;
    for(;i<100000000;i=i+1){
        true === true ? 1 : 0;
    }
console.timeEnd("TRUE test1.1")


console.time("FALSE test2.1")
    i=0;
    for(;i<100000000;i=i+1){
        false === true ? 1 : 0;
    }
console.timeEnd("FALSE test2.1")

console.log("----------------------------")

console.time("TRUE test3")
    i=0;
    for(;i<100000000;i=i+1){
        true | 0;
    }
console.timeEnd("TRUE test3")

console.time("FALSE test4")
    i=0;
    for(;i<100000000;i=i+1){
        false | 0;
    }
console.timeEnd("FALSE test4")

console.log("----------------------------")

console.time("TRUE test5")
    i=0;
    for(;i<100000000;i=i+1){
        true * 1;
    }
console.timeEnd("TRUE test5")

console.time("FALSE test6")
    i=0;
    for(;i<100000000;i=i+1){
        false * 1;
    }
console.timeEnd("FALSE test6")

console.log("----------------------------")

console.time("TRUE test7")
    i=0;
    for(;i<100000000;i=i+1){
        true & 1;
    }
console.timeEnd("TRUE test7")

console.time("FALSE test8")
    i=0;
    for(;i<100000000;i=i+1){
        false & 1;
    }
console.timeEnd("FALSE test8")

console.log("----------------------------")

console.time("TRUE test9")
    i=0;
    for(;i<100000000;i=i+1){
        +true;
    }
console.timeEnd("TRUE test9")

console.time("FALSE test10")
    i=0;
    for(;i<100000000;i=i+1){
        +false;
    }
console.timeEnd("FALSE test10")

console.log("----------------------------")

console.time("TRUE test9.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+true;
    }
console.timeEnd("TRUE test9.1")

console.time("FALSE test10.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+false;
    }
console.timeEnd("FALSE test10.1")

console.log("----------------------------")

console.time("TRUE test9.2")
    i=0;
    for(;i<100000000;i=i+1){
        -true*-1;
    }
console.timeEnd("TRUE test9.2")

console.time("FALSE test10.2")
    i=0;
    for(;i<100000000;i=i+1){
        -false*-1;
    }
console.timeEnd("FALSE test10.2")

console.log("----------------------------")

console.time("TRUE test9.3")
    i=0;
    for(;i<100000000;i=i+1){
        true-0;
    }
console.timeEnd("TRUE test9.3")

console.time("FALSE test10.3")
    i=0;
    for(;i<100000000;i=i+1){
        false-0;
    }
console.timeEnd("FALSE test10.3")

console.log("----------------------------")

console.time("TRUE test11")
    i=0;
    for(;i<100000000;i=i+1){
        Number(true);
    }
console.timeEnd("TRUE test11")

console.time("FALSE test12")
    i=0;
    for(;i<100000000;i=i+1){
        Number(false);
    }
console.timeEnd("FALSE test12")

console.log("----------------------------")

console.time("TRUE test13")
    i=0;
    for(;i<100000000;i=i+1){
        true + 0;
    }
console.timeEnd("TRUE test13")

console.time("FALSE test14")
    i=0;
    for(;i<100000000;i=i+1){
        false + 0;
    }
console.timeEnd("FALSE test14")

console.log("----------------------------")

console.time("TRUE test15")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test15")

console.time("FALSE test16")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test16")

console.log("----------------------------")

console.time("TRUE test17")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test17")

console.time("FALSE test18")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test18")

console.log("----------------------------")

console.time("TRUE test19")
    i=0;
    for(;i<100000000;i=i+1){
        true >> 0;
    }
console.timeEnd("TRUE test19")

console.time("FALSE test20")
    i=0;
    for(;i<100000000;i=i+1){
        false >> 0;
    }
console.timeEnd("FALSE test20")

console.log("----------------------------")

console.time("TRUE test21")
    i=0;
    for(;i<100000000;i=i+1){
        true >>> 0;
    }
console.timeEnd("TRUE test21")

console.time("FALSE test22")
    i=0;
    for(;i<100000000;i=i+1){
        false >>> 0;
    }
console.timeEnd("FALSE test22")

console.log("----------------------------")

console.time("TRUE test23")
    i=0;
    for(;i<100000000;i=i+1){
        true << 0;
    }
console.timeEnd("TRUE test23")

console.time("FALSE test24")
    i=0;
    for(;i<100000000;i=i+1){
        false << 0;
    }
console.timeEnd("FALSE test24")

console.log("----------------------------")

console.time("TRUE test25")
    i=0;
    for(;i<100000000;i=i+1){
        ~~true;
    }
console.timeEnd("TRUE test25")

console.time("FALSE test26")
    i=0;
    for(;i<100000000;i=i+1){
        ~~false;
    }
console.timeEnd("FALSE test26")

console.log("----------------------------")

console.time("TRUE test25.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~true*-1-1;
    }
console.timeEnd("TRUE test25.1")

console.time("FALSE test26.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~false*-1-1;
    }
console.timeEnd("FALSE test26.1")

console.log("----------------------------")

console.time("TRUE test27")
    i=0;
    for(;i<100000000;i=i+1){
        true/1;
    }
console.timeEnd("TRUE test27")

console.time("FALSE test28")
    i=0;
    for(;i<100000000;i=i+1){
        false/1;
    }
console.timeEnd("FALSE test28")

Result

TRUE test1: 93.301ms
FALSE test2: 102.854ms
----------------------------
TRUE test1.1: 118.979ms
FALSE test2.1: 119.061ms
----------------------------
TRUE test3: 97.265ms
FALSE test4: 108.389ms
----------------------------
TRUE test5: 85.854ms
FALSE test6: 87.449ms
----------------------------
TRUE test7: 83.126ms
FALSE test8: 84.992ms
----------------------------
TRUE test9: 99.683ms
FALSE test10: 87.080ms
----------------------------
TRUE test9.1: 85.587ms
FALSE test10.1: 86.050ms
----------------------------
TRUE test9.2: 85.883ms
FALSE test10.2: 89.066ms
----------------------------
TRUE test9.3: 86.722ms
FALSE test10.3: 85.187ms
----------------------------
TRUE test11: 86.245ms
FALSE test12: 85.808ms
----------------------------
TRUE test13: 84.192ms
FALSE test14: 84.173ms
----------------------------
TRUE test15: 81.575ms
FALSE test16: 81.699ms
----------------------------
TRUE test17: 81.979ms
FALSE test18: 81.599ms
----------------------------
TRUE test19: 81.578ms
FALSE test20: 81.452ms
----------------------------
TRUE test21: 115.886ms
FALSE test22: 88.935ms
----------------------------
TRUE test23: 82.077ms
FALSE test24: 81.822ms
----------------------------
TRUE test25: 81.904ms
FALSE test26: 82.371ms
----------------------------
TRUE test25.1: 82.319ms
FALSE test26.1: 96.648ms
----------------------------
TRUE test27: 89.943ms
FALSE test28: 83.646ms
DarckBlezzer
  • 3,504
  • 1
  • 33
  • 44
-1

if you want integer x value change if 1 to 0 and if 0 to 1 you can use (x + 1) % 2

blackmamba
  • 27
  • 1
  • 5