How do I create bit array in Javascript?

后端 未结 8 728
别跟我提以往
别跟我提以往 2020-12-05 04:47

What is the best way of implementing a bit array in JavaScript?

8条回答
  •  猫巷女王i
    2020-12-05 05:23

    I don't know about bit arrays, but you can make byte arrays easy with new features.

    Look up typed arrays. I've used these in both Chrome and Firefox. The important one is Uint8Array.

    To make an array of 512 uninitialized bytes:

    var arr = new UintArray(512);
    

    And accessing it (the sixth byte):

    var byte = arr[5];
    

    For node.js, use Buffer (server-side).

    EDIT:

    To access individual bits, use bit masks.

    To get the bit in the one's position, do num & 0x1

提交回复
热议问题