find the the value of given bit in javascript
I have a test which I don't understand what I have to do maybe because I didn't work a lot with bits.
the test is this:
it('you should be able to find the value of a given bit', function()
expect(answers.valueAtBit(128, 8)).to.eql(1);
expect(answers.valueAtBit(65, 1)).to.eql(1);
expect(answers.valueAtBit(65, 7)).to.eql(1);
expect(answers.valueAtBit(128, 1)).to.eql(0);
);
and this is the function that they gave to me:
valueAtBit: function(num, bit)
,
In that function I have to return the value which has to match with the tests.
javascript testing bit
add a comment |
I have a test which I don't understand what I have to do maybe because I didn't work a lot with bits.
the test is this:
it('you should be able to find the value of a given bit', function()
expect(answers.valueAtBit(128, 8)).to.eql(1);
expect(answers.valueAtBit(65, 1)).to.eql(1);
expect(answers.valueAtBit(65, 7)).to.eql(1);
expect(answers.valueAtBit(128, 1)).to.eql(0);
);
and this is the function that they gave to me:
valueAtBit: function(num, bit)
,
In that function I have to return the value which has to match with the tests.
javascript testing bit
add a comment |
I have a test which I don't understand what I have to do maybe because I didn't work a lot with bits.
the test is this:
it('you should be able to find the value of a given bit', function()
expect(answers.valueAtBit(128, 8)).to.eql(1);
expect(answers.valueAtBit(65, 1)).to.eql(1);
expect(answers.valueAtBit(65, 7)).to.eql(1);
expect(answers.valueAtBit(128, 1)).to.eql(0);
);
and this is the function that they gave to me:
valueAtBit: function(num, bit)
,
In that function I have to return the value which has to match with the tests.
javascript testing bit
I have a test which I don't understand what I have to do maybe because I didn't work a lot with bits.
the test is this:
it('you should be able to find the value of a given bit', function()
expect(answers.valueAtBit(128, 8)).to.eql(1);
expect(answers.valueAtBit(65, 1)).to.eql(1);
expect(answers.valueAtBit(65, 7)).to.eql(1);
expect(answers.valueAtBit(128, 1)).to.eql(0);
);
and this is the function that they gave to me:
valueAtBit: function(num, bit)
,
In that function I have to return the value which has to match with the tests.
javascript testing bit
javascript testing bit
asked Nov 14 '18 at 1:30
promodominuspromodominus
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can convert integers to binary strings like:
let n = 11
let bin = n.toString(2)
console.log(bin)With that you just need to get the correct character. Assuming your counting from the right (and starting at 1) you can do:
function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisAnother option if you want to do the math is to divide by the number by 2**bit and take the mod:
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53291899%2ffind-the-the-value-of-given-bit-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
You can convert integers to binary strings like:
let n = 11
let bin = n.toString(2)
console.log(bin)With that you just need to get the correct character. Assuming your counting from the right (and starting at 1) you can do:
function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisAnother option if you want to do the math is to divide by the number by 2**bit and take the mod:
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
add a comment |
You can convert integers to binary strings like:
let n = 11
let bin = n.toString(2)
console.log(bin)With that you just need to get the correct character. Assuming your counting from the right (and starting at 1) you can do:
function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisAnother option if you want to do the math is to divide by the number by 2**bit and take the mod:
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
add a comment |
You can convert integers to binary strings like:
let n = 11
let bin = n.toString(2)
console.log(bin)With that you just need to get the correct character. Assuming your counting from the right (and starting at 1) you can do:
function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisAnother option if you want to do the math is to divide by the number by 2**bit and take the mod:
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
You can convert integers to binary strings like:
let n = 11
let bin = n.toString(2)
console.log(bin)With that you just need to get the correct character. Assuming your counting from the right (and starting at 1) you can do:
function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisAnother option if you want to do the math is to divide by the number by 2**bit and take the mod:
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
let n = 11
let bin = n.toString(2)
console.log(bin)let n = 11
let bin = n.toString(2)
console.log(bin)function valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisfunction valueAtBit(num, bit)
n = num.toString(2)
return bit > n.length
? 0
: n.toString(2)[n.length - bit]
console.log(valueAtBit(11, 1))
console.log(valueAtBit(11, 2))
console.log(valueAtBit(11, 3))
console.log(valueAtBit(11, 4))
console.log(valueAtBit(11, 5)) // all zero after thisfunction valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
function valueAtBit(num, bit)
return Math.floor(num / (2 ** bit)) % 2
let num = 523
console.log("number in binary:", num.toString(2))
for (let i = 0; i < 10; i++)
console.log("bit: ", i, valueAtBit(num, i))
edited Nov 14 '18 at 3:35
answered Nov 14 '18 at 1:43
Mark MeyerMark Meyer
38.7k33159
38.7k33159
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53291899%2ffind-the-the-value-of-given-bit-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