Created
May 25, 2017 20:14
-
-
Save r3b/248636dfb9b1faa35eb7c5cd69c6f5ce to your computer and use it in GitHub Desktop.
ES5 Bitmask
This file contains 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
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