mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 13:12:42 +01:00
huge regactoring
This commit is contained in:
23
app/bitwise/calc.js
Normal file
23
app/bitwise/calc.js
Normal file
@@ -0,0 +1,23 @@
|
||||
(function(app, should){
|
||||
var calc = {};
|
||||
|
||||
calc.numberOfBits = function(num) {
|
||||
should.bePositiveInteger(num);
|
||||
return Math.floor(Math.log(num) / Math.log(2)) + 1;
|
||||
};
|
||||
|
||||
calc.maxNumberOfBits = function () {
|
||||
|
||||
var counts = [], num;
|
||||
for(var i=0;i<arguments.length; i++)
|
||||
{
|
||||
num = arguments[i];
|
||||
counts.push(this.numberOfBits(num));
|
||||
}
|
||||
|
||||
return Math.max.apply(null, counts);
|
||||
};
|
||||
|
||||
app.service('calc', calc);
|
||||
|
||||
})(window.app, window.should);
|
||||
25
app/bitwise/expression.js
Normal file
25
app/bitwise/expression.js
Normal file
@@ -0,0 +1,25 @@
|
||||
(function(bitwise) {
|
||||
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
|
||||
|
||||
app.service('expression', {
|
||||
parse: function(string) {
|
||||
var matches = twoOperandsRegex.exec(string);
|
||||
if(matches == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(matches);
|
||||
|
||||
return {
|
||||
string:matches[0],
|
||||
operand1: parseInt(matches[1], 10),
|
||||
sign: matches[2],
|
||||
operand2: parseInt(matches[3], 10),
|
||||
result: function() {
|
||||
return eval(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
})(window.app);
|
||||
28
app/bitwise/formatter.js
Normal file
28
app/bitwise/formatter.js
Normal file
@@ -0,0 +1,28 @@
|
||||
(function(should, app){
|
||||
|
||||
app.service("formatter", {
|
||||
toBinaryString: function(num, totalLength) {
|
||||
|
||||
var binaryStr = num.toString(2),
|
||||
formatted = [],
|
||||
i;
|
||||
|
||||
if(totalLength != null) {
|
||||
should.bePositiveInteger(totalLength);
|
||||
}
|
||||
|
||||
for(i = 0; i<binaryStr.length; i++) {
|
||||
formatted.push(binaryStr[i]);
|
||||
}
|
||||
|
||||
while(totalLength > formatted.length) {
|
||||
formatted.unshift('0');
|
||||
}
|
||||
|
||||
return formatted.join('');
|
||||
}
|
||||
});
|
||||
|
||||
})(window.should, window.app);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user