Allow to input of negative unsigned numbers

This commit is contained in:
BorysLevytskyi
2023-05-12 14:30:44 +02:00
parent 77a1651006
commit ba8c7d4966
7 changed files with 42 additions and 13 deletions

View File

@@ -9,6 +9,7 @@ export type BinaryStringViewProps = {
className?: string;
disableHighlight?: boolean,
signBitIndex?: number,
integerBitSize?: number
};
export type FlipBitEventArg = {
@@ -57,13 +58,17 @@ export default class BinaryStringView extends React.Component<BinaryStringViewPr
const css = allowFlipBits ? ' flipable' : ''
const disableHighlight = this.props.disableHighlight || false;
const firstBitIndex = this.props.integerBitSize != null
? bitChars.length - this.props.integerBitSize
: -1;
return bitChars.map((c, i) => {
var className = c == '1' ? `one${css}` : `zero${css}`;
var tooltip = '';
const isExtra = i < (this.props.signBitIndex || 0);
const isExtra = i < firstBitIndex;
if (isExtra)
className += ' extra-bit';

View File

@@ -1,8 +1,9 @@
const UINT8_MAX_VALUE = 255;
const UINT16_MAX_VALUE = 65535
const INT32_MAX_VALUE = 2147483647;
const INT32_MIN_VALUE = -2147483648;
const UINT32_MAX_VALUE = 4294967295;
const INT64_MAX_VALUE = BigInt("9223372036854775807");
const INT64_MIN_VALUE = BigInt("-9223372036854775808");
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 };

View File

@@ -126,6 +126,7 @@ class ExpressionElementTableRow extends React.Component<ExpressionElementRowProp
binaryString={bin}
allowFlipBits={allowFlipBits}
signBitIndex={signBitIndex}
integerBitSize={this.scalar.value.maxBitSize}
onBitClicked={args => this.onBitClicked(args)} />
</td>
<td className="other">{this.getAlternative()}</td>

View File

@@ -1,6 +1,7 @@
import exp from 'constants';
import { asIntN } from '../core/utils';
import {numberParser, ParsedNumber} from './numberParser';
import { UINT16_MAX_VALUE, UINT32_MAX_VALUE, UINT64_MAX_VALUE, UINT8_MAX_VALUE } from '../core/const';
describe("parser", () => {
@@ -124,10 +125,6 @@ describe("parser", () => {
//expect(v2).toEqual(v);
});
it('cannot parse negative usigned', () => {
expect(() => numberParser.parse('-1u')).toThrowError("-1u unsigned integer cannot be negative");
});
it('parses usigned single', () => {
var v = numberParser.parse('1us')?.value
expect(v?.maxBitSize).toBe(16);
@@ -164,4 +161,23 @@ describe("parser", () => {
//var v2 = numberParser.parse('1i16')?.value
//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);
});
});

View File

@@ -52,11 +52,17 @@ function parseInteger(input : string, numberPart: string, suffix: string) : Int
let num = BigInt(numberPart);
const signed = !suffix.startsWith('u');
if(!signed && isNegative)
throw new Error(input + " unsigned integer cannot be negative");
const bitSize = getSizeBySuffix(suffix, num, signed);
const newValue = isNegative ? -num : num;
const size = getSizeBySuffix(suffix, num, signed);
return new Integer(isNegative ? -num : num, size, signed);
if(!signed && isNegative)
{
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) {

View File

@@ -71,7 +71,7 @@ a.hashLink { font-size: 1.1em;}
.cur { color: lightgray; }
.extra-bit { opacity: 0.2;}
.extra-bit { opacity: 0.3;}
button { border: none; text-decoration: underline;}