Decompose a bitmask into bits






4.56/5 (3 votes)
A simple tool to allow you to decompose bitmask values into its bits.
Introduction
I use bitmasks far less than I used to, but when I do use them I often have the need to decompose a value into its parts.
The BitDemaskerer
Enter a bitmask
This decomposes to: 0
The Code
Super simple:
Enter a bitmask
This decomposes to: 0
<script type="text/javascript">
function GetBits() {
var hex = document.getElementById("Hex").checked;
var elm = document.getElementById("bitmask");
var value = parseInt(elm.value);
if (isNaN(value))
result.innerHTML = "Not a number";
else {
result.innerHTML = "";
for (var i = 1; i <= value; i = i * 2)
if ((value & i) > 0)
{
var bitRepresentation = hex? "0x" + i.toString(16) : i.toString();
result.innerHTML += (result.innerHTML == "" ? "" : ", ") + bitRepresentation;
}
}
}
</script>