mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 05:02:48 +01:00
Allow to input of negative unsigned numbers
This commit is contained in:
@@ -14,7 +14,7 @@ export class Integer {
|
|||||||
readonly signed: boolean;
|
readonly signed: boolean;
|
||||||
|
|
||||||
constructor(value: IntegerInput, maxBitSize?: number, signed? : boolean) {
|
constructor(value: IntegerInput, maxBitSize?: number, signed? : boolean) {
|
||||||
|
|
||||||
this.value = typeof value == "bigint" ? value : BigInt(value);
|
this.value = typeof value == "bigint" ? value : BigInt(value);
|
||||||
this.signed = signed == null ? true : signed == true;
|
this.signed = signed == null ? true : signed == true;
|
||||||
this.maxBitSize = maxBitSize != null ? maxBitSize : detectSize(this.value, this.signed);
|
this.maxBitSize = maxBitSize != null ? maxBitSize : detectSize(this.value, this.signed);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export type BinaryStringViewProps = {
|
|||||||
className?: string;
|
className?: string;
|
||||||
disableHighlight?: boolean,
|
disableHighlight?: boolean,
|
||||||
signBitIndex?: number,
|
signBitIndex?: number,
|
||||||
|
integerBitSize?: number
|
||||||
};
|
};
|
||||||
|
|
||||||
export type FlipBitEventArg = {
|
export type FlipBitEventArg = {
|
||||||
@@ -57,13 +58,17 @@ export default class BinaryStringView extends React.Component<BinaryStringViewPr
|
|||||||
const css = allowFlipBits ? ' flipable' : ''
|
const css = allowFlipBits ? ' flipable' : ''
|
||||||
|
|
||||||
const disableHighlight = this.props.disableHighlight || false;
|
const disableHighlight = this.props.disableHighlight || false;
|
||||||
|
const firstBitIndex = this.props.integerBitSize != null
|
||||||
|
? bitChars.length - this.props.integerBitSize
|
||||||
|
: -1;
|
||||||
|
|
||||||
return bitChars.map((c, i) => {
|
return bitChars.map((c, i) => {
|
||||||
|
|
||||||
var className = c == '1' ? `one${css}` : `zero${css}`;
|
var className = c == '1' ? `one${css}` : `zero${css}`;
|
||||||
var tooltip = '';
|
var tooltip = '';
|
||||||
|
|
||||||
const isExtra = i < (this.props.signBitIndex || 0);
|
|
||||||
|
const isExtra = i < firstBitIndex;
|
||||||
if (isExtra)
|
if (isExtra)
|
||||||
className += ' extra-bit';
|
className += ' extra-bit';
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
const UINT8_MAX_VALUE = 255;
|
||||||
|
const UINT16_MAX_VALUE = 65535
|
||||||
const INT32_MAX_VALUE = 2147483647;
|
const INT32_MAX_VALUE = 2147483647;
|
||||||
const INT32_MIN_VALUE = -2147483648;
|
const INT32_MIN_VALUE = -2147483648;
|
||||||
const UINT32_MAX_VALUE = 4294967295;
|
const UINT32_MAX_VALUE = 4294967295;
|
||||||
const INT64_MAX_VALUE = BigInt("9223372036854775807");
|
const INT64_MAX_VALUE = BigInt("9223372036854775807");
|
||||||
const INT64_MIN_VALUE = BigInt("-9223372036854775808");
|
const INT64_MIN_VALUE = BigInt("-9223372036854775808");
|
||||||
const UINT64_MAX_VALUE = BigInt("18446744073709551615");
|
const UINT64_MAX_VALUE = BigInt("18446744073709551615");
|
||||||
export {INT32_MAX_VALUE, INT32_MIN_VALUE, INT64_MAX_VALUE, INT64_MIN_VALUE, UINT32_MAX_VALUE, UINT64_MAX_VALUE};
|
export {INT32_MAX_VALUE, INT32_MIN_VALUE, INT64_MAX_VALUE, INT64_MIN_VALUE, UINT32_MAX_VALUE, UINT64_MAX_VALUE, UINT8_MAX_VALUE, UINT16_MAX_VALUE };
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ class ExpressionElementTableRow extends React.Component<ExpressionElementRowProp
|
|||||||
binaryString={bin}
|
binaryString={bin}
|
||||||
allowFlipBits={allowFlipBits}
|
allowFlipBits={allowFlipBits}
|
||||||
signBitIndex={signBitIndex}
|
signBitIndex={signBitIndex}
|
||||||
|
integerBitSize={this.scalar.value.maxBitSize}
|
||||||
onBitClicked={args => this.onBitClicked(args)} />
|
onBitClicked={args => this.onBitClicked(args)} />
|
||||||
</td>
|
</td>
|
||||||
<td className="other">{this.getAlternative()}</td>
|
<td className="other">{this.getAlternative()}</td>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import exp from 'constants';
|
import exp from 'constants';
|
||||||
import { asIntN } from '../core/utils';
|
import { asIntN } from '../core/utils';
|
||||||
import {numberParser, ParsedNumber} from './numberParser';
|
import {numberParser, ParsedNumber} from './numberParser';
|
||||||
|
import { UINT16_MAX_VALUE, UINT32_MAX_VALUE, UINT64_MAX_VALUE, UINT8_MAX_VALUE } from '../core/const';
|
||||||
|
|
||||||
describe("parser", () => {
|
describe("parser", () => {
|
||||||
|
|
||||||
@@ -124,10 +125,6 @@ describe("parser", () => {
|
|||||||
//expect(v2).toEqual(v);
|
//expect(v2).toEqual(v);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('cannot parse negative usigned', () => {
|
|
||||||
expect(() => numberParser.parse('-1u')).toThrowError("-1u unsigned integer cannot be negative");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('parses usigned single', () => {
|
it('parses usigned single', () => {
|
||||||
var v = numberParser.parse('1us')?.value
|
var v = numberParser.parse('1us')?.value
|
||||||
expect(v?.maxBitSize).toBe(16);
|
expect(v?.maxBitSize).toBe(16);
|
||||||
@@ -164,4 +161,23 @@ describe("parser", () => {
|
|||||||
//var v2 = numberParser.parse('1i16')?.value
|
//var v2 = numberParser.parse('1i16')?.value
|
||||||
//expect(v2?.num()).toEqual(v?.num());
|
//expect(v2?.num()).toEqual(v?.num());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('allows negative unsigned for the sake of cimplicity', () => {
|
||||||
|
|
||||||
|
const ubyte = numberParser.parse("-1ub").value;
|
||||||
|
expect(ubyte.num()).toBe(UINT8_MAX_VALUE);
|
||||||
|
expect(ubyte.maxBitSize).toBe(8);
|
||||||
|
|
||||||
|
const ushort = numberParser.parse("-1us").value;
|
||||||
|
expect(ushort.num()).toBe(UINT16_MAX_VALUE);
|
||||||
|
expect(ushort.maxBitSize).toBe(16);
|
||||||
|
|
||||||
|
const uint = numberParser.parse("-1u").value;
|
||||||
|
expect(uint.num()).toBe(UINT32_MAX_VALUE);
|
||||||
|
expect(uint.maxBitSize).toBe(32);
|
||||||
|
|
||||||
|
const ulong = numberParser.parse("-1ul").value;
|
||||||
|
expect(ulong.value).toBe(UINT64_MAX_VALUE);
|
||||||
|
expect(ulong.maxBitSize).toBe(64);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -52,11 +52,17 @@ function parseInteger(input : string, numberPart: string, suffix: string) : Int
|
|||||||
let num = BigInt(numberPart);
|
let num = BigInt(numberPart);
|
||||||
const signed = !suffix.startsWith('u');
|
const signed = !suffix.startsWith('u');
|
||||||
|
|
||||||
if(!signed && isNegative)
|
const bitSize = getSizeBySuffix(suffix, num, signed);
|
||||||
throw new Error(input + " unsigned integer cannot be negative");
|
const newValue = isNegative ? -num : num;
|
||||||
|
|
||||||
const size = getSizeBySuffix(suffix, num, signed);
|
if(!signed && isNegative)
|
||||||
return new Integer(isNegative ? -num : num, size, signed);
|
{
|
||||||
|
const signed = new Integer(newValue, bitSize, true);
|
||||||
|
const bin = "0b" + signed.toString(2);
|
||||||
|
return Integer.unsigned(BigInt(bin), bitSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Integer(newValue, bitSize, signed);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSizeBySuffix(suffix: string, value : bigint, signed: boolean) {
|
function getSizeBySuffix(suffix: string, value : bigint, signed: boolean) {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ a.hashLink { font-size: 1.1em;}
|
|||||||
|
|
||||||
.cur { color: lightgray; }
|
.cur { color: lightgray; }
|
||||||
|
|
||||||
.extra-bit { opacity: 0.2;}
|
.extra-bit { opacity: 0.3;}
|
||||||
|
|
||||||
button { border: none; text-decoration: underline;}
|
button { border: none; text-decoration: underline;}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user