0

I want to apply a bitwise operation on a value to turn it to 0 if the value is 0 or to turn it to 1 if it greater than zero.

Specifically I want a soultion in JavaScript, a way around Number(Boolean(value))

0 -> 0 1 -> 1 2 -> 1 3 -> 1 4 -> 1 ... 

can I achieve this with bitwise operations? Arithmetic operations are fine, too.

4
  • Are arithmetic or logical operators allowed (e.g. + or !), or does it need to be strictly bitwise operators only ? Commented Jun 19, 2017 at 9:13
  • Artithmetic would be fine. Essentially I want a more eloquent way around Number(Boolean(value)) Commented Jun 19, 2017 at 9:15
  • Good answers already, just for fun: 1-!x should also do the trick Commented Jun 19, 2017 at 10:01
  • Number(Boolean(x)) can be written as +!!x (unary operation) Commented Jun 19, 2017 at 10:05

2 Answers 2

4

!!x is the idiomatic way of doing this, for an integral type (including boolean) x.

Sign up to request clarification or add additional context in comments.

9 Comments

Not bitwise though (! is a logical operator).
Indeed, I assumed it was the "user doesn't quite know what they want" situation. And I'm too old to use ~ and ^.
You could be right - I've asked for clarification in the comments.
If used in JS for example a value of 3 will give me true i do not want a boolean, i want a Number
@Strernd "i want a Number" then do !!x|0 now you have bitwise operation :)
|
2

x && 1 does it.

for(x=0; x<5; x++) { console.log(x & 1) } 

Writes:

0 1 1 1 1 

This is a logical AND but Javascript's type rules appear to keep the result as a number.


But you should consider how many JS coders would instinctively understand this.

Preferring legibility to terseness, I would personally use the ternary operator:

x == 0 ? 0 : 1 

... although the option you've asked to avoid is also highly legible:

Number(Boolean(x)) 

You seem to imply that this might be slower than the alternatives.

  1. I see no reason why this would be slow. Number and Boolean are language built-ins that do very simple operations that we'd expect to be incredibly cheap
  2. Premature optimisation is the root of all evil. You should only worry about this kind of micro-optimisation if your code is running unacceptably slowly. This operation would only have such an effect if it was running millions of times in a loop. If that was the case you could easily tell if another option was cheaper, just by trying it.

Note that your requirement:

turn it to 0 if the value is 0 or to turn it to 1 if it greater than zero

... does not specify what you want to happen if x is negative. This answer allows you to tune accordingly.

2 Comments

I was just curious about if this method would be faster than using if or casting.
@Strernd both are stupidly fast. Don't optimise for speed unless you're experiencing problems with slowness and it results in a measurable improvement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.