Skip to content

Instantly share code, notes, and snippets.

@r3b
Created May 25, 2017 20:14
Show Gist options
  • Save r3b/248636dfb9b1faa35eb7c5cd69c6f5ce to your computer and use it in GitHub Desktop.
Save r3b/248636dfb9b1faa35eb7c5cd69c6f5ce to your computer and use it in GitHub Desktop.
ES5 Bitmask
function Bitmask(value) {
function parse(value){
return Number(parseInt(value)||0);
}
return Object.create({}, {
_value: {
value: parse(value),
enumerable: false,
configurable: false,
writable: true
},
set: {
value: function (value) {
this._value = parse(value);
return this._value == value;
},
enumerable: false,
configurable: false,
writable: false
},
has: {
value: function (bit) {
return (this._value & parse(bit)) == bit;
},
enumerable: false,
configurable: false,
writable: false
},
add: {
value: function (bit) {
this._value = this._value | parse(bit);
return this._value;
},
enumerable: false,
configurable: false,
writable: false
},
remove: {
value: function (bit) {
this._value = this._value & ~parse(bit);
return this._value;
},
enumerable: false,
configurable: false,
writable: false
},
toString: {
value: function (base) {
return this._value.toString(parse(base) || 10);
},
enumerable: false,
configurable: false,
writable: false
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment