How to manipulate bits in Javascript?
up vote
-2
down vote
favorite
Let's say I have a variable X = 4
How to create a number with a binary representation 1111 (ones with length X) using bitwise operators ( & | ~ << ^ ) and take a position and toggle the bit at that position to zero (0).
Example:
X = initial(4) // X should be : 1111
Y = solution(X, 2) // Y should be 1101
Z = solution(Y, 3) // Z should be 1001
javascript bit-manipulation
add a comment |
up vote
-2
down vote
favorite
Let's say I have a variable X = 4
How to create a number with a binary representation 1111 (ones with length X) using bitwise operators ( & | ~ << ^ ) and take a position and toggle the bit at that position to zero (0).
Example:
X = initial(4) // X should be : 1111
Y = solution(X, 2) // Y should be 1101
Z = solution(Y, 3) // Z should be 1001
javascript bit-manipulation
1
You can set a given bitito0withx & (~(1<<i)).
– Willem Van Onsem
Nov 10 at 13:37
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
2
@BadisMerabet:(1<<(n+1))-1.
– Willem Van Onsem
Nov 10 at 13:38
1
@WillemVanOnsem That's one1too much:console.log(((1<<(4+1))-1).toString(2)) // "11111"
– Andreas
Nov 10 at 13:41
1
@Andreas: argh, yes,(1<<4)-1.
– Willem Van Onsem
Nov 10 at 13:44
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Let's say I have a variable X = 4
How to create a number with a binary representation 1111 (ones with length X) using bitwise operators ( & | ~ << ^ ) and take a position and toggle the bit at that position to zero (0).
Example:
X = initial(4) // X should be : 1111
Y = solution(X, 2) // Y should be 1101
Z = solution(Y, 3) // Z should be 1001
javascript bit-manipulation
Let's say I have a variable X = 4
How to create a number with a binary representation 1111 (ones with length X) using bitwise operators ( & | ~ << ^ ) and take a position and toggle the bit at that position to zero (0).
Example:
X = initial(4) // X should be : 1111
Y = solution(X, 2) // Y should be 1101
Z = solution(Y, 3) // Z should be 1001
javascript bit-manipulation
javascript bit-manipulation
edited Nov 10 at 13:54
asked Nov 10 at 13:35
Badis Merabet
1,9931529
1,9931529
1
You can set a given bitito0withx & (~(1<<i)).
– Willem Van Onsem
Nov 10 at 13:37
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
2
@BadisMerabet:(1<<(n+1))-1.
– Willem Van Onsem
Nov 10 at 13:38
1
@WillemVanOnsem That's one1too much:console.log(((1<<(4+1))-1).toString(2)) // "11111"
– Andreas
Nov 10 at 13:41
1
@Andreas: argh, yes,(1<<4)-1.
– Willem Van Onsem
Nov 10 at 13:44
add a comment |
1
You can set a given bitito0withx & (~(1<<i)).
– Willem Van Onsem
Nov 10 at 13:37
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
2
@BadisMerabet:(1<<(n+1))-1.
– Willem Van Onsem
Nov 10 at 13:38
1
@WillemVanOnsem That's one1too much:console.log(((1<<(4+1))-1).toString(2)) // "11111"
– Andreas
Nov 10 at 13:41
1
@Andreas: argh, yes,(1<<4)-1.
– Willem Van Onsem
Nov 10 at 13:44
1
1
You can set a given bit
i to 0 with x & (~(1<<i)).– Willem Van Onsem
Nov 10 at 13:37
You can set a given bit
i to 0 with x & (~(1<<i)).– Willem Van Onsem
Nov 10 at 13:37
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
2
2
@BadisMerabet:
(1<<(n+1))-1.– Willem Van Onsem
Nov 10 at 13:38
@BadisMerabet:
(1<<(n+1))-1.– Willem Van Onsem
Nov 10 at 13:38
1
1
@WillemVanOnsem That's one
1 too much: console.log(((1<<(4+1))-1).toString(2)) // "11111"– Andreas
Nov 10 at 13:41
@WillemVanOnsem That's one
1 too much: console.log(((1<<(4+1))-1).toString(2)) // "11111"– Andreas
Nov 10 at 13:41
1
1
@Andreas: argh, yes,
(1<<4)-1.– Willem Van Onsem
Nov 10 at 13:44
@Andreas: argh, yes,
(1<<4)-1.– Willem Van Onsem
Nov 10 at 13:44
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
Yes, you'd use Math.pow (or on modern browsers, the exponentiation operator, **) and the bitwise operators to do that.
function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Or — doh! — comments on the question point out that you can create the initial number without Math.pow or exponentiation:
function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Yes, you'd use Math.pow (or on modern browsers, the exponentiation operator, **) and the bitwise operators to do that.
function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Or — doh! — comments on the question point out that you can create the initial number without Math.pow or exponentiation:
function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
add a comment |
up vote
4
down vote
Yes, you'd use Math.pow (or on modern browsers, the exponentiation operator, **) and the bitwise operators to do that.
function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Or — doh! — comments on the question point out that you can create the initial number without Math.pow or exponentiation:
function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
add a comment |
up vote
4
down vote
up vote
4
down vote
Yes, you'd use Math.pow (or on modern browsers, the exponentiation operator, **) and the bitwise operators to do that.
function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Or — doh! — comments on the question point out that you can create the initial number without Math.pow or exponentiation:
function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Yes, you'd use Math.pow (or on modern browsers, the exponentiation operator, **) and the bitwise operators to do that.
function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));Or — doh! — comments on the question point out that you can create the initial number without Math.pow or exponentiation:
function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));function initial(digits)
return Math.pow(2, digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));function initial(digits)
return (1 << digits) - 1;
function solution(value, bit)
return value & ~(1 << (bit - 1)); // () around `bit - 1` aren't necessary,
// but I find it clearer
var X = initial(4); // X should be : 1111
console.log(X.toString(2));
var Y = solution(X, 2); // Y should be 1101
console.log(Y.toString(2));
var Z = solution(Y, 3); // Z should be 1001
console.log(Z.toString(2));edited Nov 10 at 13:49
answered Nov 10 at 13:41
T.J. Crowder
667k11611771273
667k11611771273
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
add a comment |
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
Yes, I like the second solution. more down to earth.
– Badis Merabet
Nov 10 at 13:49
1
1
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
@BadisMerabet - Yeah, I wasn't thinking "bits" enough, but fortunately Willem Van Onsem was.
– T.J. Crowder
Nov 10 at 13:51
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239502%2fhow-to-manipulate-bits-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can set a given bit
ito0withx & (~(1<<i)).– Willem Van Onsem
Nov 10 at 13:37
ok, how to go from number 4 to 1111 ?
– Badis Merabet
Nov 10 at 13:38
2
@BadisMerabet:
(1<<(n+1))-1.– Willem Van Onsem
Nov 10 at 13:38
1
@WillemVanOnsem That's one
1too much:console.log(((1<<(4+1))-1).toString(2)) // "11111"– Andreas
Nov 10 at 13:41
1
@Andreas: argh, yes,
(1<<4)-1.– Willem Van Onsem
Nov 10 at 13:44