Given the value: 0x9e9090ab (10011110100100001001000010101011)
I want to parse the value ignoring the 32nd bit such that I end up with:
0x1e9090ab (00011110100100001001000010101011)
My attempts at doing this via a bitmask (0x9e9090ab & ~0x10000000) don't seem to be working and result in a signed negative number.
Not sure what I'm doing wrong here, so any help would be appreciated.
You need to use the bitmask ~0x80000000 instead of ~0x10000000, since 0x10000000 refers to the 29th bit. Example:
var result = 0x9e9090ab & ~0x80000000;
For me it's easier to think about this way:
0x9e9090ab & 0x7FFFFFFF
You can compare using node:
> (0xFFFFFFFF).toString(2)
'11111111111111111111111111111111'
> (0x7FFFFFFF).toString(2)
'1111111111111111111111111111111'
Related
I came across this pen when searching for a way to make a textarea auto-expand with input: https://codepen.io/vsync/pen/frudD. The code includes the line
var minRows = elm.getAttribute('data-min-rows')|0, rows;
I do not understand what the bitwise or and 0 after getting the dataset attribute does. I tried removing them, but that breaks the code.
Also, I think that the ", rows" is a remnant from a previous version of the code as "rows" is not defined until later in the code, and I can remove that and the code still appears to work (please correct if I am missing something).
The bitwise OR and 0 serves to convert the string property into a number to work properly. This can be verified by checking the type of the variable.
Usually, the stuff after the comma is used as the result, but since it is undefined, whatever comes in front is used instead (I think).
When doing this:
casperjs somescript.js --number=736280854938322517687376855643288785
and in the code:
var casper = require('casper').create();
var value = casper.cli.get("number");
console.log(value); // yields: 7.3628085493832246e+35
// want: 736280854938322517687376855643288785
I've looked and looked, pondered and hacked, but I'm not having much luck. The easy solution seems to be simply converting the number into a string. Or passing the number in as a string. But the syntax for this eludes me.
See Raw parameter values:
By default, the cli object will process every passed argument & cast them to the appropriate detected type[...]
You need to use casper.cli.raw.get("number") to get a non-parsed value. Since integer values that are bigger than 253 cannot be represented as an integer without losing precision, you would need to work with them as a string or use some big integer library (such as JSBN).
I am trying to do a simple thing but for some reason it is not working. I am using Knockout and I have a model which I update after user enter some data and use the same to communicate back to C# code on server side. For some reason, when I try to assign decimal value to one of the member of model it isn't working. Though, in this case I am using knockout, I believe it has nothing to do with KO. See the screenshot where I have the value 22.78 and I am trying to do parseFloat but it ends up as just 22. I tried other things such as removing he parseFloat just to see if it accepts the string value as it is but even that is not working. Can someone help?
Your doing Bitwise OR while assigning the value.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) | 0;
so only it returns 22. Because 22.78 | 0 is 22.
Please check this code.
console.log(22.78 | 0);
Please try this code while assigning value. You can get decimal values without loss.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED);
Pelase Check below link for more details.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR
try this (please notice the double || )
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) || 0;
So. I'm trying to subtract large integers. 76561198060995608 - 76561197960265728 = 100729880 type numbers. (I'm converting a 64 bit to a 32 bit) Vbscript and JS both give 100729888.
I would love to be able to do it in vbscript, but I'm either doing something wrong with cdbl (returns 100729888) or ccur (Overflow: 'ccur' error happens) or it can't be done the way I'm trying.
I've tried implementing JS libraries (bignum, bignumber) and they also haven't returned the correct number, again, maybe because of my error. BigNumber returns 100729890.
Big number code as follows:
$(document).ready(function(){
var x = new BigNumber(76561198060995608).subtract(new BigNumber(76561197960265728))
alert(x)
})
So...what am I doing wrong? Am I making a stupid mistake? I don't feel like this should take the 6+ hours it's taken me so far.
Any suggestions or help would be greatly appreciated. Thanks!
The problem is that when you try
new BigNumber(76561198060995608)
you're still relying on the JavaScript runtime to parse and represent that number before it calls the "BigNumber" constructor. I'm pretty sure you can pass a string to that constructor:
new BigNumber("76561198060995608")
and that should give you a fighting chance.
I'am experimenting with selenium IDE and i came across a problem with asserting an approximate value. I need to check a value inside an element with an id. It is numeric value with comma (",") as a separator.
Problem is that i need to check if the numeric value is valid with a tolerance of 0.01.
For example:
<div id="uniqueId">2,54</div>
assertText - value = 2.53
I need above example to pass the test, and also pass if the value in div si 2,52 or 2,53. I understand that i can use assertEval to insert javascript, but i'm not very good in javascript and also from what i've read the javascript capabilities of selenium are limited.
Any help would be greatly appreciated!
Using assertEval is a good idea. The javascript you will need will be something like
var numberStr = "${actualText}".replace(",", ".");
var number = parseFloat(numberStr);
var difference = Math.abs(eval(number-${expectedValue}));
(difference <= 0.01)?true:false;
I don't know much javascript but according to this thread we need to first replace decimal mark from ',' to '.' (1st line) so we can later convert the string found on page to number (2nd line).
${actualText} is a variable in which we store the actual value taken from page while the ${expectedValue} is a value you need to define on your own. Note that tolerance (0.01) is "hardcoded", you may want to replace it with variable too.
Now to make it shorter (and less readable):
(Math.abs(eval(parseFloat("${actualText}".replace(",", "."))-${expectedValue}))<=0.01)?true:false
Having the javascript we can prepare Selenium script:
storeText | id=uniqueId | actualText
store | 2.53 | expectedValue
assertEval | JS LINE FORM ABOVE GOES HERE | true