Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does number and number works in if statement?

Tags:

javascript

What does (number & number) mean? I am trying to read someone's code written in JavaScript and came over with

if(misc & 0x800) {//...} //0x800 is 2048 when converted to decimal

Where var misc = 16400; //or some other number which continuously changes

So the statement is coming like if(16400 & 2048) -do something When I console.log()ed those two numbers I got 0 .

How does if statement works in case of number and number?

like image 858
RegarBoy Avatar asked Jan 18 '26 04:01

RegarBoy


1 Answers

One & means, that this is bitwise AND

The numbers are

16400 (10) === 100000000010000 (2)

0x800 (16) === 100000000000 (2)

Based on bitwise operation rules The result will be:

100000000010000 
   100000000000
_______________
000000000000000

Such operations oftenly used for bit masks (wiki link)

like image 165
Andrey Avatar answered Jan 20 '26 19:01

Andrey