I have to store more than 30 flags(0/1) into a single integer. I can able to create 30 flag masks in Javascript using Bitwise operator like below
var FLAG_1 = 1 << 0; // 1
var FLAG_2 = 1 << 1; // 2
...
...
var FLAG_30 = 1 << 30;
var user1 = FLAG_1 | FLAG_16;
console.log(FLAG_1 & user1); // true
This works fine. But I can't create more than 30 flags because 1 << 31 goes in reverse(compliment or negative number).
Is there any way to do this ?
Yes. Using more than one 32 bit number concatenated together. If you know you need less than 64 say, use two 32 bit numbers. Take the first 32 bits and handle those flags, then separately take the second 32 and handle those.
var test = '0421';
for( var i=0; i < test.length; i += 2 ) {
flags = parseInt( test.substr(i,2), 10 );
console.log(flags);
// do comparisons here
// call function doStuff(i, flags)
}
I'm not sure what your initial input value is. I start off with a string, which can be any quantity of sets of 00 - 31. Each can can be processed by calling a function to do the work, passing the iterator and the flags.
Related
This may seem obvious, but what is exactly is an extra perfect number? I need to write an algorithm to find extra perfect for a given n, from 1 thru n. Unfortunately, I can't seem to wrap my mind around the question's wording. These are the examples given:
extraPerfect(3) ==> return {1,3}
extraPerfect(7) ==> return {1,3,5,7}
Task:
Given a positive integer N, return the extra perfect numbers in range from 1 to N.
A number is called Extra Perfect Number if it has the same first and last bits (set bits).
Notes:
Only positive integers will be passed.
The returned vector/list should contain the extra perfect numbers in
ascending order (from lowest to highest).
Example #1
extraPerfect(3) ==> return {1,3}
Explanation:
(1)10 = (1)2
First and last bits as set bits.
(3)10 = (11)2
First and last bits as set bits.
Example #2
extraPerfect(7) ==> return {1,3,5,7}
Explanation:
(5)10 = (101)2
First and last bits as set bits.
(7)10 = (111)2
First and last bits as set bits.
It seems to me that an extra perfect number is simply an odd number as, in base 2, it will always start and end with a 1, whereas an even number will always start with a 1 but end with a 0.
Ah now I see I was wrong because I thought it is all about palindroms. However I hope it can be still helpful. That's the code for palindroms in section between 1 to prompt's value.
var exns = (function(){
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
function isEXN(num){
var con = dec2bin(num); // 11011 = 3 + 24 = 27
var accurate = Math.ceil(con.length/2); // 5/2 = 3
var lenmin = con.length-1;
for(var i = 0; i < accurate; i++){
if(con.charAt(i) !== con.charAt(lenmin-i))
return false;
}
return true;
}
var max = parseInt(prompt("Numbers from 1 to ...?"));
var exns = [];
if(!isNaN(max)){
for(var i = 1; i<=max; i++){
if(isEXN(i))
exns.push(i);
}
}
return exns;
})();
Exns should contain array with values.
It looks like extraPerfect should return a list of all numbers less than the argument which have the same first and last digit after converting the decimal argument to binary.
For example:
Decimal - Binary
1 - 1
2 - 10
3 - 11
4 - 100
5 - 101
6 - 110
7 - 111
You'll notice the bold values have the same first and last binary digits.
Some pseudo-code might look like:
function extraPerfect( n ){
var perfects = [];
for(i=0; i<n; i++){
var binary = toBinary(i);
if(binary[0] === binary[binary.length]){
perfects.push(i);
}
}
return perfects;
}
You could pull an algorithm form the pseudo-code.
A Perfect Number is equal to the sum of its positive divisors.
function perfect(num){
for(var i=1,n=0; i<num; i++){
if(num % i === 0){
n += i;
}
}
return n === num;
}
console.log(perfect(6));
console.log(perfect(7));
console.log(perfect(28));
console.log(perfect(8127));
console.log(perfect(8128));
I came across this problem.
Print all possible strings that can be made by placing spaces.
I also came across this solution.
var spacer = function (input) {
var result = '' ;
var inputArray = input.split('');
var length = inputArray.length;
var resultSize = Math.pow(2,length-1); // how this works
for(var i = 0 ; i< resultSize ; i++){
for(var j=0;j<length;j++){
result += inputArray[j];
if((i & (1<<j))>0){ // how this works
result += ' ' ;
}
}
result += '\n' ;
}
return result;
}
var main = function() {
var input = 'abcd' ;
var result = spacer(input);
console.log(result);
}
main();
I am not getting how the marked lines work?
Can you clarify on what technique is being used? And what is the basic logic behind this? What are some of other areas where we can use this?
Thanks.
Let's take string abcd as an example.
There are 3 possible places where space can be put:
Between "a" and "b"
Between "b" and "c"
Between "c" and "d"
So generally if length of your string is length then you have length - 1 places for spaces.
Assume that each such place is represented with a separate digit in a binary number. This digit is 0 when we don't put space there, and 1 when we do. E.g.:
a b c d
0 0 0 means that we don't put any spaces - abcd
0 0 1 means that we put space between "c" and "d" only - abc d
0 1 0 means that we put space between "b" and "c" only - ab cd
0 1 1 means ab c d
1 0 0 means a bcd
1 0 1 means a bc d
1 1 0 means a b cd
1 1 1 means a b c d
Converting 000, 001, 010, ..., 111 from binary to decimal will give us values 0, 1, 2, ..., 7.
With 3 places for spaces we have 8 options to put them. It's exactly 2^3 or 2^(length - 1).
Thus we need to iterate all numbers between 0 (inclusive) and 2^(length - 1) (exclusive).
Condition (i & (1 << j)) > 0 in the code you provided just checks whether digit at position j (starting from the end, 0-based) is 0 (and we don't need to insert space) or 1 (and space should be added).
Let's take value 6 (110 in binary) for example.
(6 & (1 << 0)) = 110 & 001 = 000 = 0 (condition > 0 is not met)
(6 & (1 << 1)) = 110 & 010 = 010 = 2 (condition > 0 is met)
(6 & (1 << 2)) = 110 & 100 = 100 = 4 (condition > 0 is met)
simple solution Using Javascript
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
function printPattern(str, i, n){
if(i==n){
return;
}
var buff = str;
var j = str.length - n + i;
buff = str.splice(j,0," ");
console.log(buff);
printPattern(str, i+1, n);
printPattern(buff, i+1, n);
}
var str = "ABCD"
printPattern(str, 1, str.length);
console.log(str);
There are 2 possibilities between each 2 characters: either has space or not. If spaces are allowed only between characters then the number of possibilities for 4 characters is 2 * 2 * 2 or 2 ^ (length - 1)
resultSize = Math.pow(2, length - 1) says there are 2^n-1 possible ways to print a string given the problem definition. As far as to why that is the number of solutions is pretty easy to understand if you start with a string that has 2 characters and work your way upward. So pretend you have the string "ab". There is two solutions, you can put a space between a and b or you can not put a space between a and b. Lets add a character to get "abc". Well, we already know that there are two solutions for the string "ab" so we need multiply that solution by the number of ways you can put a space between b and c. Which is 2 so that gives us 2 * 2 solutions. You can extend that to a string of n size. So that means that the number of solutions is 2 * 2 * 2 * 2 * ... n - 1. Or in otherwords 2 ^ n-1.
The next part is a little trickier, but its a clever way of determining how many spaces and where they go in any given solution. The bitwise & takes the each bit of two numbers, compares them, then spits out a new number where each bit is a 1 if both the bits of the two numbers were 1, or 0 if the bits were not 1 and 1. For example (binary numbers):
01 & 01 = 01
10 & 01 = 00
Or a bigger example:
10010010 & 10100010 = 10000010
The << operator just moves all bits n position to left where n is the right hand expression (multiply by 2 n times).
For example:
1 << 1 = 2
2 << 1 = 4
2 << 2 = 8
1 << 4 = 8
So back to your code now. Lets break down the if statement
if(i & (1 << j) > 0){ ... }
In english this says, if the number index of the solution we are looking at shares any 1 bits with 1 shifted by the index of the character we are looking at, then put a space after that character. Olexiy Sadovnikov's answer has some good examples of what this would look like for some of the iterations.
Its also worth noting that this is not the only way to do this. You could pretty easily determine that the max number of spaces is n - 1 then just linearly find all of the solutions that have 0 spaces in them, then find all the solutions that have 1 space in them, then 2 spaces .... to n - 1 spaces. Though the solution you posted would be faster than doing it this way. Of course when your talking about algorithms of exponential complexity it ultimately won't matter because strings bigger than about 60 chars will take longer than you probably care to wait for even with a strictly 2^n algorithm.
In answer to the question of how these techniques can be used in other places. Bit shifting is used very frequently in encryption algorithms as well as the bitwise & and | operators.
'''
The idea was to fix each character from the beginning and print space separated rest of the string.
Like for "ABCD":
A BCD # Fix A and print rest string
AB CD # Add B to previous value A and print rest of the string
ABC D # Add C to previous value AB and print rest of the string
Similarly we can add a space to produce all permutations.
Like:
In second step above we got "AB CD" by having "A" as prefix
So now we can get "A B CD" by having "A " as a prefix
'''
def printPermute(arr, s, app):
if len(arr) <= 1:
return
else:
print(app +''+arr[0:s] +' '+ arr[s:len(arr)])
prefix = app + ''+arr[0:s]
suffix = arr[s:len(arr)]
printPermute(suffix, 1, prefix)
printPermute(suffix, 1, prefix+' ') #Appending space
printPermute("ABCDE", 1, '') #Empty string
.pow is a method from Math which stands for "power". It takes two arguments: the base (here 2) and the exponent. Read here for more information.
& is the bitwise AND operator, it takes the two binary representation of the numbers and performs a logical AND, here's a thread on bitwise operators
EDIT: why Math.pow(2,length-1) gives us the number of possible strings?
I remember doing it in an exercise last year in math class, but I'll try to explain it without sums.
Essentially we want to determine the number of strings you can make by adding one or no space in between letters. Your initial string has n letters. Here's the reason why:
Starting from the left or the word after each letter you have two choices
1 - to put a space
2 - not to put a space
You will have to choose between the two options exactly n-1 times.
This means you will have a total of 2^(n-1) possible solutions.
Looking at the int 44 — I need Math.CEIL (log(2) 44) of binary places to represent 44.
(answer is 6 places)
6 places :
___ ___ ___ ___ ___ ___
32 16 8 4 2 1
But how can I check that (for example) the bit of 8 is checked or not ?
A simple solution will be do to :
((1<<3) & 44)>0 so this will check if the bit is set.
But please notice that behind the scenes the computer translates 44 to its binary representation and just check if bit is set via bitwise operation.
Another solution is just to build the binary myself via toString(2) or mod%2 in a loop
Question
Mathematically Via which formula, I can test if n'th bit is set ?
(I would prefer a non loop operation but pure single math phrase)
Divide by the value of the bit that you want to check
and test if the first bit is set (this can be tested with x mod 2 == 1)
Math expression:
floor(value/(2^bitPos)) mod 2 = 1
As JS function:
function isSet(value, bitPos) {
var result = Math.floor(value / Math.pow(2, bitPos)) % 2;
return result == 1;
}
Note: bitPos starts with 0 (bit representing the nr 1)
The 'bit' (actually any base) value of an indexed number index in a value val in base base can in general be calculated as
val = 1966;
index = 2;
base = 10;
alert (Math.floor(val/Math.pow(base,index)) % base);
result: 9
val = 44;
index = 3;
base = 2;
alert (Math.floor(val/Math.pow(base,index)) % base);
result: 1 (only 0 and 1 are possible here – the range will always be 0..base-1).
The combination of Math.floor (to coerce to an integer in Javascript) and Math.pow is kind of iffy here. Even in integer range, Math.pow may generate a floating point number slightly below the expected 'whole' number. Perhaps it is safer to always add a small constant:
alert (Math.floor(0.1+val/Math.pow(base,index)) % base);
You can simply check if the bit at the position is set to 1.
function isBitSet(no, index) {
var bin = no.toString(2);
// Convert to Binary
index = bin.length - index;
// Reverse the index, start from right to left
return bin[index] == 1;
}
isBitSet(44, 2); // Check if second bit is set from left
DEMO
var x = 02345;
var y = x.toString();
alert(y);
I realized that there is a problem converting leading zeroes number to string in JavaScript using the toString() method.
As you can see from the output of the code above, the output is 1253 instead of the supposedly 02345.
If the leading zero is removed, the code will work as expected, why? What is happening with the code above so I can change it to work as expected.
var x = 2345;
var y = x.toString();
alert(y);
EDIT : The reason I asked this question is because I have two different codes that work differently despite being very similar. After reading that this question has nothing to do with the toString() method, why does the first set of code below not detect the number as an octal value but the second set of code does.
var num=window.prompt(); // num = 0012222
var str = num.toString();
var result = [str[0]];
for(var x=1; x<str.length; x++)
{
if((str[x-1]%2 === 0)&&(str[x]%2 === 0))
{
result.push('-', str[x]);
}
else
{
result.push(str[x]);
}
}
alert(result.join('')); //Outputs : 0-012-2-2-2
The other code :
function numberDash(num) {
var stringNumber = num.toString();
var output = [stringNumber[0]];
for (var i = 1; i < stringNumber.length; i++) {
if (stringNumber[i-1] % 2 === 0 && stringNumber[i] % 2 === 0) {
output.push('-', stringNumber[i]);
} else {
output.push(stringNumber[i]);
}
}
return output.join('');
}
numberDash(0012222) // Outputs : "52-6-6";
Many JavaScript engines add octal numeric literals to the specification. The leading zero indicates octal (base 8). 2345 in base 8 (octal) is 1253 in base 10 (decimal):
Octal Decimal
----- --------------------
2 2 * 8 * 8 * 8 = 1024
3 3 * 8 * 8 = 192
4 4 * 8 = 32
5 5
----- --------------------
2345 1253
You can disable that using strict mode. See §B.1.1 of the specification. Doing so makes 02345 a syntax error.
So it's nothing to do with toString, it's just that the 02345 in your code isn't the value you expect.
Re your updated question:
why does the first set of code below not detect the number as an octal value but the second set of code does
Because in the first code, you're not dealing with a number, you're dealing with a string. window.prompt returns a string, not a number, even if what you type in is all digits.
Even if you converted the string to a number via Number(num) or +num, the rules for runtime string->number conversion are different from the rules for parsing JavaScript source code.
var x = 02345;
var y = x.toString("8");
alert(y);
This will give you 2345
Leading zero is interpreted as octal value.
If you need number converted to string with leading zero, use my method but simply modify it like this var y = "0" + x.toString("8")
In the code below, if result is set to one, the code returns a number 1024 (2 to the power of 10). If result is set to 2, the code returns the number 2048 (or 2 to the power of 11), BUT if result is set to 3, the code doesn`t return the number 4096 (as I would expect, because 2 to the power of 12) but rather 3072. Why does it return 3072 if "result" is set to 3, but otherwise if set to 1 and 2 it followers the pattern of power to the 10th and 11th
function power(base, exponent) {
var result = 1;
for (var count = 0; count < exponent; count++)
result *= base;
return result;
}
show(power(2, 10));
In this code, result is used as an accumulator. The code computes result * base**exponent; result is not part of the exponent.
I think you're confusing yourself. The thing being raised to some power is your first parameter (in your example 2). The exponent is the 2nd parameter (10). Your example works correctly. Pass in 12 as your exponent and you will see your expected result.
The result parameter is just a place holder in which you accumulate your results (2x2x2x2....)
The numbers doing all the work are the ones you pass in as parameters to the function.
So, in your example, you're going to take 2 and multiply it by itself 10 times, each time storing the cumulative result in the variable "result". The thing that's throwing you off is that result is initially set to 1. This is never meant to be altered. It's built that way so that if you set your exponent to 0 you will end up with a result of 1 (because any number raised to the zeroth power = 1).
Anyway... don't worry about what result is set to. Focus on how the loop works with the interchangeable variable values that are being passed in, in the function call.
Do out the multiplication:
3 * 2 = 6
6 * 2 = 16
12 * 2 = 24
24 * 2 = 48
48 * 2 = 96
96 * 2 = 192
192 * 2 = 384
384 * 2 = 768
768 * 2 = 1536
1536 * 2 = 3072
Multiplication table perhaps? Also, what's wrong with Math.pow, just trying to accomplish it yourself?