1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| var BooleStr = {
_Bs1:'',
_Bs2:'',
_Rep:'',
_isBINARY: function(S) {
return (/^[0-1]*$/).test(S);
},
_prep:function(s1,s2 ) {
let l1 = s1.length, l2 = s2.length, lx = l1 - l2;
this._Rep='';
if (this._isBINARY(s1) && this._isBINARY(s2)) {
this._Bs1 = (lx < 0) ? '0'.repeat(-lx) + s1 : s1;
this._Bs2 = (lx > 0) ? '0'.repeat(lx) + s2 : s2;
return Math.max(l1, l2);
} else {
this._Rep='error: non binaire';
return 0;
}
},
_getVal:function(S,p){
let l=S.length-1;
if(p>l) return NaN;
if(this._isBINARY(S)) return ( S[l-p]==='1' );
else return 'error: non binaire';
},
_setVal:function(S,p,v){
let l=S.length-1;
this._Rep = (p>l)?'0'.repeat(p-l)+S:S;
l=Math.max(l,p) -p;
this._Rep = this._Rep.substr(0,l)+((v)?'1':'0')+this._Rep.substr(l+1);
return this._Rep;
},
_NOT: function(S) {
let l = S.length;
this._Rep='';
if (this._isBINARY(S)) {
for (let i=0; i<l; i++) {
this._Rep += (S[i]==='0')?'1':'0';
}
}
return this._Rep;
},
_AND:function(S1, S2) {
let l=this._prep(S1, S2);
for (let i=0; i<l; i++) {
this._Rep += (this._Bs1[i] === this._Bs2[i]) ? this._Bs1[i] : '0';
}
return this._Rep;
},
_OR: function(S1, S2) {
let l=this._prep(S1, S2);
for (let i=0; i<l; i++) {
this._Rep += (this._Bs1[i] === '1' || this._Bs2[i] === '1') ? '1' : '0';
}
return this._Rep;
},
_NOR: function(S1, S2) {
let l=this._prep(S1, S2);
for (let i=0; i<l; i++) {
this._Rep += (this._Bs1[i] === '0' && this._Bs2[i] === '0') ? '1' : '0';
}
return this._Rep;
},
_XOR: function(S1, S2) {
let l=this._prep(S1, S2);
for (let i=0; i<l; i++) {
this._Rep += (this._Bs1[i]==='1' || this._Bs2[i]==='1')?((this._Bs1[i] != this._Bs2[i]?'1':'0')):'0';
}
return this._Rep;
}
};
console.log('1010 AND 1101 => ' + BooleStr._AND ('1010', '1101') ); // 1000
console.log('1a10 OR 1101 => ' + BooleStr._OR ('1a10', '1101') ); // error: non binaire
console.log('1010 OR 1101 => ' + BooleStr._OR ('1010', '1101') ); // 1111
console.log('1010 XOR 1101 => ' + BooleStr._XOR ('1010', '1101') ); // 0111
console.log(' 1 OR 1101 => ' + BooleStr._OR ( '1', '1101') ); // 1101
console.log(' 1 NOR 1101 => ' + BooleStr._NOR ( '1', '1101') ); // 0010
console.log('1000 XOR 1 => ' + BooleStr._XOR ('1000', '1') ); // 1001
console.log('binaire ? =fg01e= ' + BooleStr._isBINARY ('f01ghe' )); // false
console.log('binaire ? =01001= ' + BooleStr._isBINARY ('01001' )); // true
console.log('val binaire pos=0 00001 ' + BooleStr._getVal ('00001',0 )); // true
console.log('val binaire pos=2 00100 ' + BooleStr._getVal ('00100',2 )); // true
console.log('val binaire pos=4 00100 ' + BooleStr._getVal ('00100',4 )); // false
console.log('val binaire pos=8 00100 ' + BooleStr._getVal ('00100',8 )); // NaN
console.log('setVal("00100",1,true) -> '+ BooleStr._setVal ('00100', 1, true )); // 00110
console.log('setVal("00100",12,true) -> '+ BooleStr._setVal ('00100', 12, true )); // 1000000000100
console.log(' _NOT =01001= ' + BooleStr._NOT ('01001' )); // 10110 |