Setting specific bits in a number

10,214

Solution 1

The easiest way to clear a bit, is to use the and operation with it's bit complement.

7 =  0000000000000111
~2 = 1111111111111101
& :  0000000000000101

In code:

var d = 7, mask = 2;
d &= ~mask;

To set a bit instead of clearing it, you use the or operator instead:

d |= mask;

If you need to create the mask dynamically to handle different bits, you start with the value one (binary 0000000000000001) and shift the bit to the correct index. The second bit has index one (the rightmost bit has index zero), so:

var index = 1;
var mask = 1 << index;

Solution 2

One way to do it, but you'd probably be better using bitwise operators

var d = 7;
var binary = d.toString(2);

binary = binary.split('');
binary[1] = "0";
binary = binary.join('');
binary = parseInt(binary,2);

Solution 3

To set the second bit, simply OR with 2 (10 in binary)

var d=5;
var mask=2;
var second_bit_set=d | mask;


          d: 101
       mask: 010
 --------------------
 bitwise OR: 111

To remove the second bit, you want to AND with a value which has all bits set, apart from the second one. An easy way to construct this value is to perform bitwise NOT on the value, e.g. ~2

var d=7;
var mask=~2;
var second_bit_unset=d & mask;

           d: 111
        mask: 101
 --------------------
 bitwise AND: 101

See this bitwise operator reference for more information on these operators.

Solution 4

/** Convert a decimal number to binary **/

var toBinary = function(decNum){
    return parseInt(decNum,10).toString(2);
}

/** Convert a binary number to decimal **/

var toDecimal = function(binary) {
    return parseInt(binary,2).toString(10);
}
Share:
10,214
omg
Author by

omg

Updated on June 07, 2022

Comments

  • omg
    omg almost 2 years

    var d = 7;

    in binary: 7 = (111)

    What I want to do is to set second place from right to 1 or 0 at disposal,

    and return the decimal value.

    For example,if I want to make the second 1 to 0,then after process should return a 5,

    because 5=(101).

    How to implement this in javascript?

    EDIT

    the answer should be something like this:

    function func(decimal,n_from_right,zero_or_one)
    {
    
    }
    

    Where decimal is the number to be processed, n_from_right is how many bits from right,in my example above it's 2. zero_or_one means to set that specific bit to 0 or 1 at disposal.

  • kliron
    kliron over 14 years
  • Amit Patil
    Amit Patil over 14 years
    I agree bitwise arithmetic is best, but +1 for pointing out the relatively little-known way to convert to/from binary strings.
  • Michael Theriot
    Michael Theriot almost 9 years
    Am I misunderstanding? ~2 !== parseInt("1111111111111101", 2)
  • Guffa
    Guffa almost 9 years
    @MichaelTheriot: I simplified a bit in the answer, the bitwise operators works on 32 bit numbers, so it should be another 16 1's to be accurate. However, the numbers are still not the same, because ~2 is -3 and parseInt("11111111111111111111111111111101", 2) is 4294967293. As the bitwise operators works on 32 bit numbers, and 4294967293 is too large to fit in 32 bits, it will be converted to -3 in the operation. (Well, it's really converted to the binary representation of the number, but it comes out as -3 when converted back.)