25.09.2026 - fixed bug in binary calculator!

This commit is contained in:
2026-09-25 14:21:08 +02:00
parent 5cdadd4e49
commit 2c204a2f3b
6 changed files with 79 additions and 13 deletions
+28 -13
View File
@@ -126,9 +126,9 @@ function tryParseInt(str) {
function flipBinString(remainderString)
{
let count = remainderString.length;
let remainders = [count];
let remainders = [];
for(i = 0; i < count; i++)
for(let i = 0; i < count; i++)
{
remainders[i] = remainderString[i];
}
@@ -143,14 +143,26 @@ function additionTwoBinNumbers(bin1, bin2)
let bin1Array = binArrays.first;
let bin2Array = binArrays.second;
let binOverflow = Array.from({length: bin2Array.length}, (v, k) => '0');
let lastOverflowBit = '0';
let tempResult = "";
for(i = bin1Array.length-1; i >= 0; i--){
if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '0')
function setOverflow(index){
if(isLastIndex(index))
{
lastOverflowBit = '1';
}else{
binOverflow[index-1] = '1';
}
}
for(let i = bin1Array.length-1; i >= 0; i--){
if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '0') {
tempResult += '0';
}else if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '1')
{
tempResult += '1';
}else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '0')
{
tempResult += '1';
@@ -158,20 +170,20 @@ function additionTwoBinNumbers(bin1, bin2)
else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '1')
{
tempResult += '0';
binOverflow[i-1] = '1';
setOverflow(i);
}
else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '0')
{
tempResult += '0';
binOverflow[i-1] = '1';
setOverflow(i);
}else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '1')
{
tempResult += '1';
binOverflow[i-1] = '1';
setOverflow(i);
}
}
if(binOverflow[-1] === '1')
if(lastOverflowBit === '1')
{
tempResult += '1';
}
@@ -180,6 +192,10 @@ function additionTwoBinNumbers(bin1, bin2)
return tempResult;
}
function isLastIndex(index){
return index === 0;
}
function fillwithZero(binArray1, binArray2)
{
let difference = Math.abs(binArray1.length - binArray2.length);
@@ -188,14 +204,14 @@ function fillwithZero(binArray1, binArray2)
{
if(binArray1.length < binArray2.length)
{
for(i = 0; i < difference; i++)
for(let i = 0; i < difference; i++)
{
binArray1.unshift('0');
}
}
else
{
for(i = 0; i < difference; i++)
for(let i = 0; i < difference; i++)
{
binArray2.unshift('0');
}
@@ -205,5 +221,4 @@ function fillwithZero(binArray1, binArray2)
first: binArray1,
second: binArray2
}
}