Understanding bitwise operations in javascript

后端 未结 4 1521
时光说笑
时光说笑 2020-12-15 01:36

I am currently storing data inside an XML doc as binary, 20 digits long, each representing a boolean value.



        
4条回答
  •  [愿得一人]
    2020-12-15 02:38

    Assuming you have no more than 32 bits, you can use JavaScript's built-in parseInt() function to convert your string of 1s and 0s into an integer, and then test the flags using the & (and) operator:

    var flags = parseInt("10001010100011110000", 2); // base 2
    
    if ( flags & 0x1 )
    {
       // do something
    }
    
    ...
    

    See also: How to check my byte flag?

    (question is on the use in C, but applies to the same operators in JS as well)

提交回复
热议问题