Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Javascript

Decompose a bitmask into bits

Rate me:
Please Sign up or sign in to vote.
4.56/5 (3 votes)
4 Aug 2015CPOL 13.8K   3
A simple tool to allow you to decompose bitmask values into its bits.

This article may contain live JavaScript that has been reviewed and tested for security. If you wish to submit articles containing JavaScript please email your submissions to submit@codeproject.com.

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:

HTML
<p>Enter a bitmask <input id="bitmask" onkeyup="GetBits()" style="width:100px;font-size:20px" type="text" />  
<input type="radio" id="Dec" name="outputFormat" value="dec" checked="true" onchange="GetBits()" /><label for="Dec">Decimal</label>
<input type="radio" id="Hex" name="outputFormat" value="hex" onchange="GetBits()" /><label for="Hex">Hexadecimal</label>
</p>

<p>This decomposes to: <span id="result">0</span></p>

<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>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Yvan Rodrigues10-Aug-15 8:18
professionalYvan Rodrigues10-Aug-15 8:18 
GeneralRe: My vote of 5 Pin
Chris Maunder10-Aug-15 9:07
cofounderChris Maunder10-Aug-15 9:07 
AnswerRe: My vote of 5 Pin
Yvan Rodrigues10-Aug-15 9:11
professionalYvan Rodrigues10-Aug-15 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.