JavaScript Match on Binary bits - Case statement will not work

"Off Topic" as this seems to be a JavaScript problem
I am expecting only the first case statement to be hit in the code below.
But all of them are hit.
Why?
I know there are no break statements that is on purpose.
I want code to be run if the relevant bit is set.
In the example below only bit 1 is set.
If the variable is, say 3, (Binary 0011) then I want the fist 2 case statements to hit.
Etc.

function OnClick(clickData)
{
	// --------------------------------------------------------
	DOpus.ClearOutput();
	// --------------------------------------------------------
	var FeatureMask = 0x1;
	DOpus.Output("000-010 FeatureMask START            :" + ZeroPad(FeatureMask.toString(2),4));
	DOpus.Output("")


        switch(FeatureMask)
        {
            case (FeatureMask&0x1):
                DOpus.Output("     004000-200-000  Case FeatureMask&0x1    ---C =  " + ZeroPad(FeatureMask&0x1.toString(2),4));
                DOpus.Output("     004000-200-000  Case FeatureMask        ---C =  " + ZeroPad(FeatureMask.toString(2),4));
                DOpus.Output("");
            case (FeatureMask&0x2):
                DOpus.Output("     004000-220-000  Case FeatureMask&0x2    --B- =  " + ZeroPad(FeatureMask&0x2.toString(2),4));
                DOpus.Output("     004000-220-000  Case FeatureMask        --B- =  " + ZeroPad(FeatureMask.toString(2),4));
                DOpus.Output("");
            case (FeatureMask&0x4):
                DOpus.Output("     004000-240-000  Case FeatureMask&0x4    -A-- =  " + ZeroPad(FeatureMask&0x4.toString(2),4));
                DOpus.Output("     004000-240-000  Case FeatureMask        -A-- =  " + ZeroPad(FeatureMask.toString(2),4));
                DOpus.Output("");
            case (FeatureMask&0x8):
                DOpus.Output("     004000-280-000  Case FeatureMask&0x8    F--- =  " + ZeroPad(FeatureMask&0x8.toString(2),4));
                DOpus.Output("     004000-280-000  Case FeatureMask        F--- =  " + ZeroPad(FeatureMask.toString(2),4));
                DOpus.Output("");
            default:
                DOpus.Output("     004000-400-010  FeatureMask Default          =  " + ZeroPad(FeatureMask.toString(2),4));
        }
	// --------------------------------------------------------
}

function ZeroPad(s,c)
{
	s = s + "";
	while(s.length < c)
	{
		s = "0" + s;
	}
	return s;
}

In this example below

1 Like

Case statements according to ChatGPT will not do it.
It suggested the if construct below.
The explanation was flawed but good enough that I could work out that it was never going to work.
Fun claim - according to this guy for the last several years the about of compute expended or made available on AI ahs increased by a factor of 10.
So over 5 years that would be 10x10x10x10x10=100000 time capacity.
A sort of new Moore's Law and it is continuing.

I am starting to get real sue from ChatGTP to troubleshoot bits of my code....
Full ChatGTP output below from anyone interested.....

Your original code is missing all the breaks at the end of each case.

It’s often better to look up how things work than ask ChatGPT to guess. :slight_smile:

Thanks @Leo
ChatGPT will only help real dumb programmers I think - like me :slight_smile:

It's been some days, but next time, you could do it like this? o)

if (FeatureMask & 1) { // first bit
    //..
}
if (FeatureMask & 2) { // 2nd
    //..
}
if (FeatureMask & 4) { // 3rd
    //..
}
if (FeatureMask & 8) { // 4th
    //..
}
if (FeatureMask & 16) { // 5th
    //..
}

If you want only one condition to be met at a time:

if (0);
else if (FeatureMask & 1) { // first bit
    //..
}
else if (FeatureMask & 2) { // 2nd
    //..
}
else if (FeatureMask & 4) { // 3rd
    //..
}
else if (FeatureMask & 8) { // 4th
    //..
}
else if (FeatureMask & 16) { // 5th
    //..
}
1 Like