mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2026-01-19 18:32:36 +01:00
Fix issue with large binary numbers (#43)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import NumericOperand from './NumericOperand';
|
||||
import NumericOperand, { INT_MAX_VALUE } from './NumericOperand';
|
||||
|
||||
it('parsed from string', () => {
|
||||
var op = NumericOperand.parse('123');
|
||||
@@ -10,4 +10,17 @@ it('can get other kind', () => {
|
||||
var op = new NumericOperand(10, 'dec');
|
||||
expect(op.getOtherBase('hex')).toBe('dec');
|
||||
expect(op.getOtherBase('bin')).toBe('hex');
|
||||
});
|
||||
});
|
||||
|
||||
it('negtive value binary string', () => {
|
||||
expect(NumericOperand.toBaseString(-1, 'bin')).toBe('11111111111111111111111111111111');
|
||||
});
|
||||
|
||||
it('64 bit operand binary string', () => {
|
||||
expect(NumericOperand.toBaseString(68719476735, 'bin')).toBe('111111111111111111111111111111111111');
|
||||
});
|
||||
|
||||
it('throws on negative 64 bit numbers', () => {
|
||||
var bigN = -(INT_MAX_VALUE+1);
|
||||
expect(() => new NumericOperand(bigN)).toThrowError("BitwiseCmd currently doesn't support 64 bit negative numbers such as " + bigN);
|
||||
})
|
||||
@@ -3,6 +3,10 @@ import { ExpressionInputItem, NumberBase } from './expression-interfaces';
|
||||
|
||||
var globalId : number = 1;
|
||||
|
||||
const INT_MAX_VALUE = 2147483647;
|
||||
|
||||
export {INT_MAX_VALUE};
|
||||
|
||||
// Represents numeric value
|
||||
export default class NumericOperand implements ExpressionInputItem {
|
||||
id: number;
|
||||
@@ -17,6 +21,9 @@ export default class NumericOperand implements ExpressionInputItem {
|
||||
this.base = base || "dec";
|
||||
this.lengthInBits = NumericOperand.getBitLength(this.value);
|
||||
this.isExpression = false;
|
||||
|
||||
if(value < 0 && !NumericOperand.is32Bit(value))
|
||||
throw new Error("BitwiseCmd currently doesn't support 64 bit negative numbers such as " + value);
|
||||
}
|
||||
|
||||
getLengthInBits() {
|
||||
@@ -102,7 +109,10 @@ export default class NumericOperand implements ExpressionInputItem {
|
||||
var hexVal = Math.abs(value).toString(16);
|
||||
return value >= 0 ? '0x' + hexVal : '-0x' + hexVal;
|
||||
case 'bin':
|
||||
return (value>>>0).toString(2);
|
||||
// >>> 0 is used to get an actual bit representation of the negative numbers
|
||||
// https://stackoverflow.com/questions/5747123/strange-javascript-operator-expr-0
|
||||
const is32Bit = NumericOperand.is32Bit(value);
|
||||
return (is32Bit && value < 0 ? (value >>> 0) : value).toString(2);
|
||||
case 'dec':
|
||||
return value.toString(10);
|
||||
default:
|
||||
@@ -113,4 +123,8 @@ export default class NumericOperand implements ExpressionInputItem {
|
||||
static toHexString (hex : string) {
|
||||
return hex.indexOf('-') === 0 ? '-0x' + hex.substr(1) : '0x' + hex;
|
||||
};
|
||||
|
||||
static is32Bit(n: number) {
|
||||
return Math.abs(n) <= INT_MAX_VALUE
|
||||
}
|
||||
}
|
||||
@@ -85,8 +85,9 @@ class ExpressionRow extends React.Component<ExpressionRowProps> {
|
||||
</tr>;;
|
||||
}
|
||||
|
||||
getBinaryString() : string {
|
||||
return this.props.expressionItem.evaluate().toBinaryString();
|
||||
getBinaryString() : string {
|
||||
var binary = this.props.expressionItem.evaluate().toBinaryString();
|
||||
return binary;
|
||||
}
|
||||
|
||||
getLabel(): string {
|
||||
|
||||
Reference in New Issue
Block a user