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
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/2aAPC.iml" filepath="$PROJECT_DIR$/.idea/2aAPC.iml" />
</modules>
</component>
</project>
Generated
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MessDetectorOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCSFixerOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCodeSnifferOptionsConfiguration">
<option name="highlightLevel" value="WARNING" />
<option name="transferred" value="true" />
</component>
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" />
</component>
</project>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+28 -13
View File
@@ -126,9 +126,9 @@ function tryParseInt(str) {
function flipBinString(remainderString) function flipBinString(remainderString)
{ {
let count = remainderString.length; 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]; remainders[i] = remainderString[i];
} }
@@ -143,14 +143,26 @@ function additionTwoBinNumbers(bin1, bin2)
let bin1Array = binArrays.first; let bin1Array = binArrays.first;
let bin2Array = binArrays.second; let bin2Array = binArrays.second;
let binOverflow = Array.from({length: bin2Array.length}, (v, k) => '0'); let binOverflow = Array.from({length: bin2Array.length}, (v, k) => '0');
let lastOverflowBit = '0';
let tempResult = ""; let tempResult = "";
for(i = bin1Array.length-1; i >= 0; i--){ function setOverflow(index){
if(isLastIndex(index))
if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '0')
{ {
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'; 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') }else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '0')
{ {
tempResult += '1'; 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') else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '1')
{ {
tempResult += '0'; tempResult += '0';
binOverflow[i-1] = '1'; setOverflow(i);
} }
else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '0') else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '0')
{ {
tempResult += '0'; tempResult += '0';
binOverflow[i-1] = '1'; setOverflow(i);
}else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '1') }else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '1')
{ {
tempResult += '1'; tempResult += '1';
binOverflow[i-1] = '1'; setOverflow(i);
} }
} }
if(binOverflow[-1] === '1') if(lastOverflowBit === '1')
{ {
tempResult += '1'; tempResult += '1';
} }
@@ -180,6 +192,10 @@ function additionTwoBinNumbers(bin1, bin2)
return tempResult; return tempResult;
} }
function isLastIndex(index){
return index === 0;
}
function fillwithZero(binArray1, binArray2) function fillwithZero(binArray1, binArray2)
{ {
let difference = Math.abs(binArray1.length - binArray2.length); let difference = Math.abs(binArray1.length - binArray2.length);
@@ -188,14 +204,14 @@ function fillwithZero(binArray1, binArray2)
{ {
if(binArray1.length < binArray2.length) if(binArray1.length < binArray2.length)
{ {
for(i = 0; i < difference; i++) for(let i = 0; i < difference; i++)
{ {
binArray1.unshift('0'); binArray1.unshift('0');
} }
} }
else else
{ {
for(i = 0; i < difference; i++) for(let i = 0; i < difference; i++)
{ {
binArray2.unshift('0'); binArray2.unshift('0');
} }
@@ -205,5 +221,4 @@ function fillwithZero(binArray1, binArray2)
first: binArray1, first: binArray1,
second: binArray2 second: binArray2
} }
} }