Created
May 29, 2017 14:36
-
-
Save brunoczim/54e530832da2b886b47a2a1e6ff9c849 to your computer and use it in GitHub Desktop.
A wrapper for byte handling in JS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
function Byte(bin) { | |
this.set = function(val) { | |
if (val instanceof Byte) { | |
val = val.get(); | |
} | |
bin = Byte.fit(val); | |
return this; | |
}; | |
this.get = function() { | |
return bin; | |
}; | |
this.set(bin); | |
} | |
Byte.fit = function(bin) { | |
return ((bin >>> 0) % 256) >>> 0; | |
}; | |
Object.defineProperty(Byte, "BIT_LENGTH", { | |
configurable: false, | |
writable: false, | |
enumerable: true, | |
value: 8 | |
}); | |
Byte.prototype.valueOf = function() { | |
return this.get(); | |
}; | |
Byte.prototype.toString = function() { | |
var str = ""; | |
for (var i = Byte.BIT_LENGTH; i > 0; i--) { | |
str += this.get() >> i - 1 & 1; | |
} | |
return str; | |
}; | |
Byte.prototype.toBoolean = function() { | |
return this.get() != 0; | |
}; | |
Byte.prototype['+'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() + val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['-'] = function() { | |
var newbyte = new Byte; | |
if (arguments.length) { | |
newbyte.set(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() - val); | |
} | |
} else { | |
newbyte.set(-this.get()); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['*'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() * val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['/'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() / val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['%'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() % val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['**'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(Math.pow(newbyte.get(), val)); | |
} | |
return newbyte; | |
}; | |
Byte.prototype.sqrt = function() { | |
return new Byte(Math.sqrt(this.get())); | |
}; | |
Byte.prototype['^'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() ^ val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['&'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() & val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['|'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() | val); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['>>'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() >> (val % Byte.BIT_LENGTH)); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['<<'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() << (val % Byte.BIT_LENGTH)); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['>>>'] = function() { | |
var newbyte = new Byte(this.get()); | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
newbyte.set(newbyte.get() >>> (val % Byte.BIT_LENGTH)); | |
} | |
return newbyte; | |
}; | |
Byte.prototype['~'] = function() { | |
return new Byte(~this.get()); | |
}; | |
Byte.prototype['!'] = function() { | |
return new Byte(this.get() == 0 ? 1 : 0); | |
}; | |
Byte.prototype['=='] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() != Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype['!='] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() == Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype['>'] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() <= Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype['>='] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() < Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype['<'] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() >= Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype['<='] = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var val = arguments[i] instanceof Byte ? arguments[i].get() : arguments[i]; | |
if (this.get() > Byte.fit(val)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Byte.prototype.add = Byte.prototype['+']; | |
Byte.prototype.subtract = Byte.prototype['-']; | |
Byte.prototype.multiply = Byte.prototype['*']; | |
Byte.prototype.divide = Byte.prototype['/']; | |
Byte.prototype.remain = Byte.prototype['%']; | |
Byte.prototype.raise = Byte.prototype['**']; | |
Byte.prototype.xor = Byte.prototype['^']; | |
Byte.prototype.and = Byte.prototype['&']; | |
Byte.prototype.or = Byte.prototype['|']; | |
Byte.prototype.reverse = Byte.prototype['~']; | |
Byte.prototype.rightShift = Byte.prototype['>>']; | |
Byte.prototype.leftShift = Byte.prototype['<<']; | |
Byte.prototype.fillAndRightShift = Byte.prototype['>>>']; | |
Byte.prototype.not = Byte.prototype['!']; | |
Byte.prototype.isEqualTo = Byte.prototype['==']; | |
Byte.prototype.isNotEqualTo = Byte.prototype['!=']; | |
Byte.prototype.isGreaterThan = Byte.prototype['>']; | |
Byte.prototype.isGreaterOrEqualTo = Byte.prototype['>=']; | |
Byte.prototype.isLessThan = Byte.prototype['<']; | |
Byte.prototype.isLessOrEqualTo = Byte.prototype['<=']; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment