Skip to content

Commit

Permalink
Constrained operands to specified bit length
Browse files Browse the repository at this point in the history
  • Loading branch information
M4444 committed Nov 5, 2017
1 parent 989d0c9 commit f1c9e20
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Binary file modified dist/C41C.jar
Binary file not shown.
47 changes: 41 additions & 6 deletions src/c41c/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -1136,16 +1136,21 @@ private void BUTTON_AddDigitActionPerformed(java.awt.event.ActionEvent evt) {//G
if (DivisionByZero)
return;
JButton button = (JButton) evt.getSource();
BigInteger newValue;
if(!OperationUnderway) {
Value = Value.multiply(new BigInteger(Integer.toString(Base)));
Value = Value.add(new BigInteger(button.getName(), Base));
newValue = Value.multiply(new BigInteger(Integer.toString(Base)))
.add(new BigInteger(button.getName(), Base));
if (newValue.compareTo(CurrentMaxInt) < 0)
Value = newValue;
} else {
if (!SecondOperandEntered) {
SecondOperand = BigInteger.ZERO;
SecondOperandEntered = true;
}
SecondOperand = SecondOperand.multiply(new BigInteger(Integer.toString(Base)));
SecondOperand = SecondOperand.add(new BigInteger(button.getName(), Base));
newValue = SecondOperand.multiply(new BigInteger(Integer.toString(Base)))
.add(new BigInteger(button.getName(), Base));
if (newValue.compareTo(CurrentMaxInt) < 0)
SecondOperand = newValue;
}

refreshTextArea();
Expand Down Expand Up @@ -1197,10 +1202,13 @@ private void bitSwitch(JLabel bitLabel) {
String bit = bitLabel.getText();
int bitPos = Integer.parseInt(bitLabel.getName());

if(!OperationUnderway)
if(!OperationUnderway) {
Value = Value.flipBit(bitPos);
else
Value = adjustForOverflow(Value);
} else {
SecondOperand = SecondOperand.flipBit(bitPos);
SecondOperand = adjustForOverflow(SecondOperand);
}
bitLabel.setText(bit.equals("1") ? "0" : "1");

//setSize(new java.awt.Dimension(386, 396));
Expand Down Expand Up @@ -1263,6 +1271,18 @@ private void ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
private void changeNumberOfBits(int bitNum) {
if (bitNum < 0)
return;
else if (bitNum == 0)
CurrentMaxInt = BigInteger.ONE;
else
CurrentMaxInt = BigInteger.ONE.shiftLeft(bitNum-1);
Value = Value.mod(CurrentMaxInt.shiftLeft(1));
Value = adjustForOverflow(Value);
if(OperationUnderway) {
SecondOperand = SecondOperand.mod(CurrentMaxInt.shiftLeft(1));
SecondOperand = adjustForOverflow(SecondOperand);
}
refreshTextArea();

int rowNum = (int) Math.ceil(bitNum/32.0);
//setSize(386+16, 378+38+23*(rowNum-1));
setSize(386+6, 378+28+23*(rowNum-1));
Expand Down Expand Up @@ -1364,9 +1384,23 @@ private void performOperation() {
default:
return;
}
Value = adjustForOverflow(Value);

refreshTextArea();
}

private BigInteger adjustForOverflow(BigInteger value) {
BigInteger overflow = value.subtract(CurrentMaxInt);
BigInteger underflow = value.add(CurrentMaxInt);

if (overflow.compareTo(BigInteger.ZERO) >= 0)
value = overflow.subtract(CurrentMaxInt);
else if (underflow.compareTo(BigInteger.ZERO) < 0)
value = underflow.add(CurrentMaxInt);

return value;
}

private void refreshTextArea() {
String out = Value.toString(Base);
if (OperationUnderway)
Expand Down Expand Up @@ -1502,6 +1536,7 @@ public void run() {
// Bits
private static final int TOTAL_BIT_NUM = 256;
private JLabel[] LABEL_bitGroup;
BigInteger CurrentMaxInt;

private ArrayList<JButton> numberButtons = new ArrayList<JButton>();
}

0 comments on commit f1c9e20

Please sign in to comment.