Why is the bitwise operator returning me 0 in this case?

Asked

Viewed 82 times

-2

Well, according to the website Imasters they define the utility of the bitwise operator and as:

The operator & ( Bitwise AND ) compares two values using their binary representations, returning a new value. To form this return value, each bit is compared, returning 1( true ) when both bits are equal to 1( true ); otherwise, returns 0( false ).

So far so good, but note this example:

let n1 = 1; // 00110001
let n2 = 2; // 00110010

console.log(n1 & n2);

The above example has the return 0 (false), that’s because 1 has the binary representation 00110001 and the 2 has binary representation 00110010, then both are not equal, but in the example below will return 1 (true), because they both contain the same binary representation:

let n1 = 1; // 00110001
let n2 = 1; // 00110001

console.log(n1 & n2);

But in the example below it will return 0 (false):

let n1 = 0; // 00110000
let n2 = 0; // 00110000

console.log(n1 & n2);

Because the return is 0 (false) if both contain the same binary representation?

  • 2

    A hint to see the binary representation of a number: n1.toString(2). If you want with the zeroes on the left, you can do n1.toString(2).padStart(8, "0")

  • hkotsubo, but what this command does padStart(8, "0")

  • Put the zeros in front, completing a maximum of 8 characters.

  • Thank you you are the guy!

1 answer

3


There are several misconceptions in your question:

1) the binary representation of 1 is not 00110001, but yes 00000001, even for 2 00000010), needs to review how the binary base works and how to convert;

2) the operators bitwise do not return a boolean but a new number, so it will never be true or false the answer;

3) The link you passed seems not to explain well the operation, so much so that confused you, I suggest the Mozilla.org, see this link: https://developer.mozilla.org/

The operations of bitwise compare the bit-by-bit numbers and result in a new number. The operation bitwise or | buy the bits, and returns 1 whenever any of the bits compared is 1, and the operation bitwise and & returns 1 whenever both bits are 1.

Let’s take a practical example to better understand this. Let’s use the numbers 1, 2 and 3:

let n1 = 1; // 00000001
let n2 = 2; // 00000010
let n3 = 3; // 00000011

See beside the binary representation of each one. If we compare n1 and N2, we will have the following results:

let n1 = 1; // 00000001
let n2 = 2; // 00000010  
-----------------------  
bitwise or |   00000011 = 3 (nesse caso, o penúltimo e o último bits são um)
bitwise and &  00000000 = 0 (neste caso, não houve ambos bits 1)

In the bitwise or, bits 7 and 8 are 1, in at least one of the numbers, therefore resulted the 00000011.
In the bitwise and, despite having bits 1, there was no bit that was 1 in both (n1 has bit 8 in 1, and N2 has bit 7).

Now comparing N2 and N3:

let n2 = 2; // 00000010
let n3 = 3; // 00000011
----------------------------  
bitwise or |   00000011 = 3 (neste caso, mesmo resultado da comparação anterior)  
bitwise and &  00000010 = 2 (neste caso, como ambos os bits 7 estão em 1, resulta 1)

Here, in the bitwise and, will return 00000010, ie number 2. Here is the practical example:

let n1 = 1; // 00000001
let n2 = 2; // 00000010
let n3 = 3; // 00000011

console.log(n1 | n2);
console.log(n1 & n2);
console.log(n2 | n3);
console.log(n2 & n3);

  • sorry I’m learning Javascript now, in the case that you spoke "the binary representation of 1 is not 00110001, but yes 00000001", so the number, for example, that I hit on this site will not be binary? https://www.invertexto.com/codigo-binario

  • 2

    @draw This site codes text, then type 1 it will interpret as a character, whose value in the ascii table is 49, which in binary becomes 110001

  • valeu helped a lot

  • as @hkotsubo mentioned this site converts text to binary, not numbers, to convert numbers you can use a calculator, or some other online site, for example: https://clevert.com.br/t/pt-br/base-convert

  • Thanks, @Ricardo Punctual, this site will help me a lot, all the best for you!

  • if the answer answered your question do not forget to accept ;)

  • Yes, I forgot! :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.