Function to convert Number to Binary string - javascript

Writing a function to convert passed in number to a binary string. The function is creating a proper binary sequence, but my compare function is skipping the first index when comparing a number equal to binaryIndex[0] (ex. n = 32, 16, 8, 4). Any ideas why?
This step creates a binary ordered array, which is what I will use to check the passed in parameter with:
var Bin = function(n) {
var x =1;
var binSeq=[];
var converted=[];
for (var i=0; x <= n; i++) {
binSeq.unshift(x)
x = x+x
}
console.log(binSeq)
This next step should compare and spit out a binary sequence of 1's and 0's: but it is skipping if (n === binSeq[0])
for (var i=0; i < binSeq.length; i++) {
if ((n - binSeq[i]) >= 0) {
converted.unshift(1);
n=n-binSeq[i]
} else {converted.unshift(0)}
}
console.log(converted)
}
Link to the CodePen: https://codepen.io/fdeppe/pen/GEozKY?editors=1111

Actually this would do the trick
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
Explanation here ==> Negative numbers to binary string in JavaScript
-3 >>> 0 (right logical shift) coerces its arguments to unsigned integers, which is why you get the 32-bit two's complement representation of -3.

Related

Is the Reverse Bits Solution Using Left Shift Results correct

Trying to solve Reverse Bits Solution Using Left Shift Results ,problem says
Reverse bits of a given 32 bits unsigned integer.
Input: n = 00000010100101000001111010011100
Output: 964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Here in the solution code loops 32 times,then doing left shifting of result,and then if num & 1 is more than 0 i.e. its 1.then increment the result and also right shift nums by 1 or nums modulus 2 and then finally return the result
why the output coming as 0,any thoughts and updated solution for this code
let reverseBits = function(nums) {
let result = 0
for (let i = 1; i <= 32; i++) {
result <<= 1
if (nums & 1 > 0)
result++
nums >>= 1
}
return result
}
console.log(reverseBits(11111111111111111111111111111101))
Output is shown as 0
PS C:\VSB-PRO> node Fibo.js
0
Some issues:
The example value you pass as argument to your function, is not given in binary notation, but in decimal notation, so it is a different number than intended. Use the 0b prefix for literals in binary notation.
When using the << operator (and =<<), JavaScript will interpret the 32nd bit as a sign bit. I suppose it is not intended to produce negative values, so avoid this by using a multiplication by 2 instead of the shift operator.
Not a problem, but:
The >> operator will have a specific effect on numbers that have the 32nd bit set: that bit will be retained after the shift. As your script never inspects that bit, it is not a problem, but it would be more natural if 0 bits were shifted-in. For that you can use the >>> operator.
Finally, it may be useful to output the return value in binary notation so you can more easily verify the result.
let reverseBits = function(nums) {
let result = 0;
for (let i = 1; i <= 32; i++) {
// use multiplication to avoid sign bit interpretation
result *= 2;
if (nums & 1 > 0)
result++;
nums >>>= 1;
}
return result;
}
// Express number in binary notation:
let n = 0b11111111111111111111111111111101;
let result = reverseBits(n);
// Display result in binary notation
console.log(result.toString(2));

Go returns other sum using bitwise operator than javascript

I've tried to make port of function made in JS using Go but I'm facing strange problem. The goal of function is to sum ascii codes from every letter in string.
Everything is fine until string length is <= 6 After that Go returns other results.
Original from JS
function c(e) { // e is string
var t = 0;
if (!e) // if e == ""
return t;
for (var n = 0; n < e.length; n++) {
t = (t << 5) - t + e.charCodeAt(n),
t &= t
}
return t
}sd
c("Google") // returns 2138589785
c("Google1") // returns 1871773944
Port in Go
package main
import (
"fmt"
)
func main() {
fmt.Println(CountChars("Google")) // returns 2138589785
fmt.Println(CountChars("Google1")) // returns 66296283384
}
func CharCodeAt(s string) int {
return int([]rune(s)[0])
}
func CountChars(char string) int {
var sum int = 0
if char == "" {
return sum
}
for x:=0; x<len(char); x++ {
charToCode := string(char[x])
sum = (sum << 5) - sum + CharCodeAt(charToCode)
sum &= sum
}
return sum
}
Go playground
JS playground in playcode
Integers in Javascript are 32-bit, while Go's int is architecture dependent, may be 32 bit and 64 bit. It's 64-bit on the Go Playground. And since each iteration shifts left by 5, using more than 6 characters surely "overflows" in Javascript (but not yet in Go): 7*5=35 > 32 bits.
Use explicit 32-bit integers (int32) to have the same output as in Javascript:
func CountChars(char string) int32 {
var sum int32 = 0
if char == "" {
return sum
}
for x := 0; x < len(char); x++ {
sum = (sum << 5) - sum + int32(char[x])
sum &= sum
}
return sum
}
This way output will be the same as that of Javascript (try it on the Go Playground):
2138589785
1871773944
Also note that Go stores strings as their UTF-8 byte sequences in memory, and indexing a string (like char[x]) indexes its bytes, the UTF-8 sequence. This is fine in your example as all the input characters are encoded using a single byte, but you'll get different result if the input contains multi-byte characters.
To properly handle all cases, use a simple for range over the string: that returns the successive runes, which is also an alias to int32, so you get the code points you need.
Also that check for empty string is unnecessary, if it's empty, the loop body will not be executed. Also sum &= sum: this is a no-op, simply remove this.
The simplified version:
func CountChars(s string) (sum int32) {
for _, r := range s {
sum = (sum << 5) - sum + r
}
return
}
Testing it:
fmt.Println(CountChars("Google 世界"))
Will output the same as in Javascript (try this one on the Go Playground):
-815903459
It's a sizing issue. JS ints are 32 bits and Go ints are not necessarily 32 bits. If you are relying on a specific int size you should specify it by replacing instances of int with int32.
What that looks like (playground):
package main
import (
"fmt"
)
func main() {
fmt.Println(CountChars("Google"))
fmt.Println(CountChars("Google1"))
}
func CharCodeAt(s string, n int) int32 {
return int32(s[n])
}
func CountChars(char string) int32 {
var sum int32 = 0
if char == "" {
return sum
}
for x:=0; x<len(char); x++ {
sum = (sum << 5) - sum + CharCodeAt(char, x)
sum &= sum
}
return sum
}

Determine number of leading zeros in a floating point number

How can I calculate how many zeros come after the decimal point but before the first non-zero in a floating point number. Examples:
0 -> 0
1 -> 0
1.0 -> 0
1.1 -> 0
1.01 -> 1
1.00003456 ->4
Intuitively I assume there is a math function that provides this, or at least does the main part. But I can neither recall nor figure out which one.
I know it can be done by first converting the number to a string, as long as the number isn't in scientific notation, but I want a pure math solution.
In my case I don't need something that works for negative numbers if that's a complication.
I'd like to know what the general ways to do it are, irrespective of language.
But if there is a pretty standard math function for this, I would also like to know if JavaScript has this function.
As a sidenote, I wonder if this calculation is related to the method for determining how many digits are required for the decimal representation of an integer.
Let x be a non-whole number that can be written as n digits of the whole part, then the decimal point, then m zeroes, then the rest of the fractional part.
x = [a1a2...an] . [0102...0m][b1b2...bm]
This means that the fractional part of x is larger than or equal to 10–m, and smaller than 10–m+1.
In other words, the decimal logarithm of the fractional part of x is larger than or equal to –m, and smaller than –m+1.
Which, in turn, means that the whole part of the decimal logarithm of the fractional part of x equals –m.
function numZeroesAfterPoint(x) {
if (x % 1 == 0) {
return 0;
} else {
return -1 - Math.floor(Math.log10(x % 1));
}
}
console.log(numZeroesAfterPoint(0));
console.log(numZeroesAfterPoint(1));
console.log(numZeroesAfterPoint(1.0));
console.log(numZeroesAfterPoint(1.1));
console.log(numZeroesAfterPoint(1.01));
console.log(numZeroesAfterPoint(1.00003456));
As a sidenote, I wonder if this calculation is related to the method for determining how many digits are required for the decimal representation of an integer.
In the same manner, a positive integer x takes n decimal digits to represent it if and only if n - 1 <= log10(x) < n.
So the number of digits in the decimal representation of x is floor(log10(x)) + 1.
That said, I wouldn't recommend using this method of determining the number of digits in practice. log10 is not guaranteed to give the exact value of the logarithm (not even as exact as IEEE 754 permits), which may lead to incorrect results in some edge cases.
You can do it with a simple while loop:
function CountZeros(Num) {
var Dec = Num % 1;
var Counter = -1;
while ((Dec < 1) && (Dec > 0)) {
Dec = Dec * 10;
Counter++;
}
Counter = Math.max(0, Counter); // In case there were no numbers at all after the decimal point.
console.log("There is: " + Counter + " zeros");
}
Then just pass the number you want to check into the function:
CountZeros(1.0034);
My approach is using a while() loop that compares the .floor(n) value with the n.toFixed(x) value of it while incrementing x until the two are not equal:
console.log(getZeros(0)); //0
console.log(getZeros(1)); //0
console.log(getZeros(1.0)); //0
console.log(getZeros(1.1)); //0
console.log(getZeros(1.01)); //1
console.log(getZeros(1.00003456)); //4
function getZeros(num) {
var x = 0;
if(num % 1 === 0) return x;
while(Math.floor(num)==num.toFixed(x)) {x++;}
return(x-1);
}
You can do it with toFixed() method, but there is only one flaw in my code, you need to specify the length of the numbers that comes after the point . It is because of the way the method is used.
NOTE:
The max length for toFixed() method is 20, so don't enter more than 20 numbers after . as said in the docs
var num = 12.0003400;
var lengthAfterThePoint = 7;
var l = num.toFixed(lengthAfterThePoint);
var pointFound = false;
var totalZeros = 0;
for(var i = 0; i < l.length; i++){
if(pointFound == false){
if(l[i] == '.'){
pointFound = true;
}
}else{
if(l[i] != 0){
break;
}else{
totalZeros++;
}
}
}
console.log(totalZeros);
Extra Answer
This is my extra answer, in this function, the program counts all the zeros until the last non-zero. So it ignores all the zeros at the end.
var num = 12.034000005608000;
var lengthAfterThePoint = 15;
var l = num.toFixed(lengthAfterThePoint);
var pointFound = false;
var theArr = [];
for(var i = 0; i < l.length; i++){
if(pointFound == false){
if(l[i] == '.'){
pointFound = true;
}
}else{
theArr.push(l[i]);
}
}
var firstNumFound = false;
var totalZeros = 0;
for(var j = 0; j < theArr.length; j++){
if(firstNumFound == false){
if(theArr[j] != 0){
firstNumFound = true;
totalZeros = totalZeros + j;
}
}else{
if(theArr[j] == 0){
totalZeros++;
}
}
}
var totalZerosLeft = 0;
for (var k = theArr.length; k > 0; k--) {
if(theArr[k -1] == 0){
totalZerosLeft++;
}else{
break;
}
}
console.log(totalZeros - totalZerosLeft);

How to convert hex to decimal WITH A LOOP in JavaScript

Is it possible to convert a hex number to a decimal number with a loop?
Example: input "FE" output "254"
I looked at those questions :
How to convert decimal to hex in JavaScript?
Writing a function to convert hex to decimal
Writing a function to convert hex to decimal
Writing a function to convert hex to decimal
How to convert hex to decimal in R
How to convert hex to decimal in c#.net?
And a few more that were not related to JS or loops. I searched for a solution in other languages too in case that I find a way to do it,but I didn't. The first one was the most useful one. Maybe I can devide by 16,compare the result to preset values and print the result, but I want to try with loops. How can I do it?
Maybe you are looking for something like this, knowing that it can be done with a oneliner (with parseInt)?
function hexToDec(hex) {
var result = 0, digitValue;
hex = hex.toLowerCase();
for (var i = 0; i < hex.length; i++) {
digitValue = '0123456789abcdef'.indexOf(hex[i]);
result = result * 16 + digitValue;
}
return result;
}
console.log(hexToDec('FE'));
Alternative
Maybe you want to have a go at using reduce, and ES6 arrow functions:
function hexToDec(hex) {
return hex.toLowerCase().split('').reduce( (result, ch) =>
result * 16 + '0123456789abcdefgh'.indexOf(ch), 0);
}
console.log(hexToDec('FE'));
Just another way to do it...
// The purpose of the function is to convert Hex to Decimal.
// This is done by adding each of the converted values.
function hextoDec(val) {
// Reversed the order because the added values need to 16^i for each value since 'F' is position 1 and 'E' is position 0
var hex = val.split('').reverse().join('');
// Set the Decimal variable as a integer
var dec = 0;
// Loop through the length of the hex to iterate through each character
for (i = 0; i < hex.length; i++) {
// Obtain the numeric value of the character A=10 B=11 and so on..
// you could also change this to var conv = parseInt(hex[i], 16) instead
var conv = '0123456789ABCDEF'.indexOf(hex[i]);
// Calculation performed is the converted value * (16^i) based on the position of the character
// This is then added to the original dec variable. 'FE' for example
// in Reverse order [E] = (14 * (16 ^ 0)) + [F] = (15 * (16 ^ 1))
dec += conv * Math.pow(16, i);
}
// Returns the added decimal value
return dec;
}
console.log(hextoDec('FE'));
Sorry that was backwards, and I can't find where to edit answer, so here is corrected answer:
function doit(hex) {
var num = 0;
for(var x=0;x<hex.length;x++) {
var hexdigit = parseInt(hex[x],16);
num = (num << 4) | hexdigit;
}
return num;
}
If you want to loop over every hex digit, then just loop from end to beginning, shifting each digit 4 bits to the left as you add them (each hex digit is four bits long):
function doit(hex) {
var num = 0;
for(var x=0;x<hex.length;x++) {
var hexdigit = parseInt(hex[x],16);
num = (num << 4) | hexdigit;
}
return num;
}
JavaScript can natively count in hex. I'm finding out the hard way that, in a loop, it converts hex to decimal, so for your purposes, this is great.
prepend your hex with 0x , and you can directly write a for loop.
For example, I wanted get an array of hex values for these unicode characters, but I am by default getting an array of decimal values.
Here's sample code that is converting unicode hex to dec
var arrayOfEmojis = [];
// my range here is in hex format
for (var i=0x1F600; i < 0x1F64F; i++) {
arrayOfEmojis.push('\\u{' + i + '}');
}
console.log(arrayOfEmojis.toString()); // this outputs an array of decimals

What is the fastest way to count the number of significant digits of a number?

What is the fastest way to count the number of significant digits of a number?
I have the following function, which works, but is quite slow due to string operations.
/**
* Count the number of significant digits of a number.
*
* For example:
* 2.34 returns 3
* 0.0034 returns 2
* 120.5e+3 returns 4
*
* #param {Number} value
* #return {Number} The number of significant digits
*/
function digits (value) {
return value
.toExponential()
.replace(/e[\+\-0-9]*$/, '') // remove exponential notation
.replace( /^0\.?0*|\./, '') // remove decimal point and leading zeros
.length
};
Is there a faster way?
Update: here a list of assertions to test correct functioning:
assert.equal(digits(0), 0);
assert.equal(digits(2), 1);
assert.equal(digits(1234), 4);
assert.equal(digits(2.34), 3);
assert.equal(digits(3000), 1);
assert.equal(digits(0.0034), 2);
assert.equal(digits(120.5e50), 4);
assert.equal(digits(1120.5e+50), 5);
assert.equal(digits(120.52e-50), 5);
assert.equal(digits(Math.PI), 16);
My own method failed for digits(0), I fixed that by adding a ? to the second regexp.
Here's a more mathematical way of doing the same operation (which appears to be significantly faster)
JSPerf comparing the three implementations
Accurate for integer n < +-(2^53) per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
Floats are converted to a string and then coerced to an int (by removing the decimal so similar rules apply)
var log10 = Math.log(10);
function getSignificantDigitCount(n) {
n = Math.abs(String(n).replace(".", "")); //remove decimal and make positive
if (n == 0) return 0;
while (n != 0 && n % 10 == 0) n /= 10; //kill the 0s at the end of n
return Math.floor(Math.log(n) / log10) + 1; //get number of digits
}
Slight improvement of regular expression
function digits (value) {
return value
.toExponential()
.replace(/^([0-9]+)\.?([0-9]+)?e[\+\-0-9]*$/g, "$1$2")
.length
};
And yet another approach, that uses string operations and handles some special cases for better performance:
function digits(value) {
if (value === 0) {
return 0;
}
//create absolute value and
var t1 = ("" + Math.abs(value));
//remove decimal point
var t2 = t1.replace(".","");
//if number is represented by scientific notation,
//the places before "e" (minus "-" and ".") are the
//significant digits. So here we can just return the index
//"-234.3e+50" -> "2343e+50" -> indexOf("e") === 4
var i = t2.indexOf("e");
if (i > -1) {
return i;
}
//if the original number had a decimal point,
//trailing zeros are already removed, since irrelevant
//0.001230000.toString() -> "0.00123" -> "000123"
if (t2.length < t1.length) {
// -> remove only leading zeros
return t2.replace(/^0+/,'').length;
}
//if number did not contain decimal point,
//leading zeros are already removed
//000123000.toString() -> "123000"
// -> remove only trailing zeros
return t2.replace(/0+$/,'').length;
}
You can directly examine the bytes of a floating-point value by using typed arrays. The advantages of doing this are that it's fast, and it doesn't require any math to be done. You can look directly at the bits of the mantissa.
You can start with this:
var n = yourFloatingPointValue;
var f64 = new Float64Array(1);
var dv = new DataView(f64.buffer);
dv.setFloat64(0, n, false); // false -> big-endian
var bytes = [];
for (var i = 0; i < 8; i++)
bytes.push(dv.getUint8(i));
Now the bytes array contains integers representing the 8-bit values of the floating point value as it looks in memory. The first byte contains the sign bit in the top bit position, and the first 7 bits of the exponent in the rest. The second byte contains the 5 least-significant bits of the exponent and the first three bits of the mantissa. The rest of the bytes are all mantissa.
Regular string checking. A slight of improvement though.
function digits(value) {
value = "" + value;
var res = 0;
for (var i = 0, len = value.length; i < len; i++){
if (value[i]==="e")break;
if (+value[i]>=0)
res++;
}
return res;
};
jsperf Benchmark testing result as compared to the OP's and other answers code.
Update
function digits(value) {
console.log(value);
value = "" + (+value);
var res = 0;
for (var i = 0, len = value.length; i < len; i++) {
if (value[i] === "e")
{
break;
}
if (+value[i] >= 0)
{
res++;
}
}
console.log(value);
return res;
}
function check(val1, val2) {
console.log( val1+"==="+val2 +" = "+ (val1 === val2));
return val1 === val2;
}
check(digits(0), 1);
check(digits(2), 1);
check(digits(1234), 4);
check(digits("0012003400"), 8);
check(digits("0022.002200"), 6);
check(digits(2.34), 3);
check(digits(3000), 4);
check(digits(0.0034), 2);
check(digits(12003), 5);
check(digits(1.23e+50), 3);
check(digits("1.23e+50"), 3);
check(digits(120.5e51), 4);
check(digits(1120.5e+52), 5);
check(digits(120.52e-53), 5);
check(digits(Math.PI), 16);
There is a faster and indirect way to do it, which is converting it to a string and finding the length of it.
a = 2.303
sig_fig = len(str(a))-len(str(int(a)))-1
The extra -1 is for the "."

Categories