How to manipulate bits in Javascript?









up vote
-2
down vote

favorite
1












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









share|improve this question



















  • 1




    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






  • 2




    @BadisMerabet: (1<<(n+1))-1.
    – Willem Van Onsem
    Nov 10 at 13:38






  • 1




    @WillemVanOnsem That's one 1 too 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














up vote
-2
down vote

favorite
1












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









share|improve this question



















  • 1




    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






  • 2




    @BadisMerabet: (1<<(n+1))-1.
    – Willem Van Onsem
    Nov 10 at 13:38






  • 1




    @WillemVanOnsem That's one 1 too 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












up vote
-2
down vote

favorite
1









up vote
-2
down vote

favorite
1






1





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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 13:54

























asked Nov 10 at 13:35









Badis Merabet

1,9931529




1,9931529







  • 1




    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






  • 2




    @BadisMerabet: (1<<(n+1))-1.
    – Willem Van Onsem
    Nov 10 at 13:38






  • 1




    @WillemVanOnsem That's one 1 too 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




    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






  • 2




    @BadisMerabet: (1<<(n+1))-1.
    – Willem Van Onsem
    Nov 10 at 13:38






  • 1




    @WillemVanOnsem That's one 1 too 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












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));








share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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));








share|improve this answer






















  • 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














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));








share|improve this answer






















  • 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












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));








share|improve this answer














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));






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric