I have a uint8 array that in the first position have the value: 0xb00000000. If i console.log(uint8[0]) i receive 0 in the console.
I know if i do uint8[0] = uint8[0] ^ 0b00000001, the last bit will change to 1, then if i execute again console.log(uint8[0]) the console logs 1.
My question is: How can i get all 256 possible values using that same way to change the bits?
Is there a formule to do this?
Basically i need the console to show:
1
2
3
4
5
...
Related
I need to store a large collection of small objects (chord diagrams) that will not change. It is a javascript project.
I have two questions:
how should I store these objects? As json, as text,... ?
what's the fastest way to find a specific item?
I search the item by it's key + type + "/" + bass:
Example: I get Am7/C# and I need to find the corresponding diagram. The key would be the file.
For now it's using only one file and search it with regex:
"{define: C frets x 3 2 0 1 0 fingers 0 3 2 0 1 0}",
"{define: C(add9) frets x 3 2 0 3 0 fingers 0 2 1 0 3 0}",
I will have 90 000 chords that I can split in 12 files (one for each key).
My object can look like this:
{type="m" bass="" frets="x 3 1 0 1 3" fingers="0 3 1 0 2 4" variation="1"}
I read a bit about binary search but I don't know if this can help me.
Thanks!
I'm making this acres and karats calculator for my uncle to help him in his work.
I'll explain the whole idea of this thing with this example. So if you add 3.22 + 2.2 it should be = 5.42 but in this calculator 3.22 + 2.2 should = 6, because 3 acres + 2 acres = 5 acres and 22 karats + 2 karats = 1 acre, so the total would be 6 acres.
The way I'm doing it in the code is that I'm splitting a number like 3.22 to two, 3 and 22 and the other number to 2 and 2 and I add the whole numbers together and the fractions together and if the fractions are >= 24 I add one to the whole numbers and if there're fractions left from the whole calculation I leave it. For example 3.15 + 2.15 = 6.6, but I'm stuck on how I can add the numbers, there's also an error in there that I don't know how to resolve.
Anyway here's the code
function getValue(v) {
return +v.toString().match(/\.(\d*)/)[1] || 0;
}
function getTotal() {
d += Math.floor(num);
p += getValue(num);
if (p >= 24) {
p -= 24;
++d;
}
total = d + p / 100;
ptag.textContent = total;
}
I added the part of the code where I'm stuck.
Note: I'm trying to make the thing able to add multiple numbers not only two. Also I'm trying to add subtraction but I have no idea how to start working on the subtraction because I haven't even finished the addition.
If the error you are talking about is something like this:
Uncaught TypeError: Cannot read property '1' of null
It is because of your getValue function.
My suggestion is, instead of using something as complicated as
function getValue(v) {
return +v.toString().match(/\.(\d*)/)[1] || 0;
}
use
function getValue(v) {
return floor((v % 1) * 100);
}
This has the same effect as the code you wrote. Which for example, from input 3.13, returns 13.
But there are few other problems.
First, you should update your num variable every now and often, otherwise, it is always going to stay as an empty string (you only defined it on line 20, and you didn't update it after that).
Second, you should clear the d and p variable after you use. As of right now, both of these variables just keeps on increasing every time you run the getTotal function
For your question of how you can add two numbers, I suggest you to create a variable where you can store the first number that the user typed.
For example, when the user typed in 4.19 and pressed the plus button, save that 4.19 into a variable (let's say firstNum).
Then when the user pressed equal button, add the number from the current input field with the firstNum variable.
On how exactly you are going to add two different numbers, break two numbers you want to add into Acres part and Karats parts. Then add them separately, then use your getTotal.
So if the number is 3.21 and 5.18, add 3 and 5, add 21 and 18, then add both of them.
you'll get 8.39. Finally, convert 8.39 into 9.15.
Sorry if my calculation is not correct. It is my first time with this concept!
But I believe this is the way to go.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I find that algorithm/functionality in two games already, but I always wanted to know what was the logic behind it.
Basically, there is a list of items and each of them has an id.
For example:
item_1 has id: 1
item_2 has id: 2
item_3 has id: 4
item_4 has id: 8
item_5 has id: 16
etc.
The id is multiplied by two every new item.
There is then a number, let's say 4, that indicate what the current item is. Is this case that would be item_3, but the tricky part is that number could also select multiple items at once like 7 which is 4 + 2 + 1 (item_3, item_2, item_1) or 17 which is 16 + 1 (item_5, item_1). It can go really high like 16384 if you have a long list and still be perfectly accurate for the multiple selections.
How do I solve this problem?
The algorithm you described is basically outputting where the 1's are in the binary representation of the number.
For 7, its binary representation is 111. There are three 1's: in the first, second, and third position from the left respectively, so it's item 1, 2 and 3. Note that we are counting from the left.
Another example:
For 10, its binary representation is 1010. There are two 1's: in the second and fourth position from the left, so the output would be items 2 and 4.
Here is an implementation in C#.
public static List<int> FindOnes(int number) {
var list = new List<int>();
var binaryString = Convert.ToString(number, 2);
for (int i = 0 ; i < binaryString.Length ; i++) {
if (binaryString[binaryString.Length - i - 1] == '1') {
list.Add(i + 1);
}
}
return list;
}
// usage:
FindOnes(7) // [1,2,3]
No idea how the games you're talking about implement it, but if this was me I would do it using bits in the binary expression of the number (example code in java).
public boolean isItemSelected(final int number, final int itemId) {
return (number & (1 << (itemId - 1))) != 0;
}
The trick here being that the binary representation of a number (from right to left) already denotes whether 1, 2, 4, 8, 16, etc. is required additively to make the number using only powers of two. The left shift simply makes a number which (in binary) is all 0's except a 1 in the 'itemId - 1'th slot. The & will match if that bit is 1 in the given number. And then checking that the result is not 0 simply turns it into a boolean.
Obviously you can combine this with some looping or anything else if you want to build the array/List of all the 'itemIds' which match.
In Javascript, you could take the number, convert it to a binary value, take the bits, reverse it and take the values (index plus one) or zero for a filtering of truthy values.
var value = 13,
items = [...value.toString(2)].reverse().map((v, i) => +v && (i + 1)).filter(Boolean);
console.log(items);
foo.setAttribute("item-position", ""+bar+"");
The bar variable is a number, for example 1 or 15. What's the way to increase it on 1, so it would be 2 and 16 ?
May be something like this? But it doesn't work.
foo.setAttribute("item-position", ""+bar+""+1);
// The result should be 2 and 16, but here the result is 21 and 161.
// That is not what I want.
You're currently appending the number to the end of the string, this has nothing to do with arithmetic.
Just add the calculated result
foo.setAttribute("item-position", bar+1);
You don't have to turn it into a string, setAttribute will do that part.
Or if you want to increase the value in bar and show it, use the preincrement operator:
foo.setAttribute("item-position", ++bar);
So, I have a list of items and I want to check how many pages I would need if I want to show , say, 10 items per page
ie
220 should give me 22 pages
134 should give me 14 pages
I am using the ceiling function to get the number of pages
var pages = parseInt(items)/10;
alert("pages " +pages);
alert(Math.ceil(pages));
The problem which I am having is if I have 7 items, I am expecting it to give me 1 page. However, I don't get any output.
I have noticed that I only get an output if the value for pages from
var pages = parseInt(items)/10;
is greater than 1 , How do I fix this problem?
I think your problem lies elsewhere. Consider the following Math.ceil operations:
> Math.ceil(220/10)
22
> Math.ceil(134/10)
14
> Math.ceil(7/10)
1
Then look at the operation that you might have handled -- a string version of a number:
> Math.ceil(parseInt("7")/10);
1
> Math.ceil(parseInt(" 7")/10);
1
> Math.ceil(parseInt(" 7 ")/10);
1
It would appear that Math.ceil is providing 1 as expected, unless your value of 7 is malformed.