// Version 11.10.2021: Lisätty tuki positiivisille kokonaisluvuille
// Version 10.11.2021: Lisätty jonkinlainen selvennys muistibitin lisäämisestä kahden bitin yhteenlaskuun
radiobtnneg = document.getElementById("choice-neg");
radiobtnneg.checked = true;
function dec2bin(dec, numbits, neg) {
if (dec.length == 0) {
return "";
}
if (neg) {
alaraja = -Math.pow(2, numbits - 1);
ylaraja = Math.pow(2, numbits - 1) - 1;
} else {
alaraja = 0;
ylaraja = Math.pow(2, numbits) - 1;
}
if (dec >= alaraja && dec <= ylaraja) {
var out = "";
var length = parseInt(numbits);
while (length--) {
out += (dec >> length) & 1;
}
return out;
} else {
return "";
}
}
function bin2dec(bin, neg) {
if (bin.length == 0) {
return "";
}
if (bin[0] == 0 || !neg) { // check sign, bin is string so left-most 'bit' (MSB) is indexed by [0]
return parseInt(bin, 2);
} else { // negative, i.e. bin[0] == 1 (MSB)
var dectemp = parseInt(bin, 2);
return ((dectemp ^ parseInt((new Array(dectemp.toString(2).length + 1)).join("1"), 2)) + 1) * -1;
}
}
var timeouts = Array(); // place timeouts in array, so they can be deleted if laske() is called during calculation (init and start new)
function additionperbit(eka, toka, viive, numbits, neg) {
(function myLoop(i, eka, toka, tulos, carry, viive) {
timeouts.push(setTimeout(function() {
// FullAdder :)
tulos += ((eka & 0x1) ^ (toka & 0x1) ^ (parseInt(carry, 2) & 0x1)).toString(); // parseInt for carry, for some reason +16 bits does not work with string
carry += (((eka & 0x1) & (toka & 0x1)) | (((eka & 0x1) ^ (toka & 0x1)) & (parseInt(carry, 2) & 0x1))).toString();
// check for overflow, with 2's complement that is when carry_in to MSB position (i==1) equals 1
// Jos kyseessä negatiivisen ja positiivisen luvun yhteenlasku, niin ylivuotobitti jätetään huomioimatta silloin kun
// negatiivisen luvun itseisarvo pienempi tai yhtäsuuri kuin...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.