Help with using bit masking

Hi! I am having some problem understanding bit masking…hoping to get some help.

here is the method i came up with to change color of the texture based on bit pattern:
note:
texture dimensions are 4 x 4
int 12 corresponds to 1100 so my understanding was that lines should yield following:

(bits & 1) == 1 => false
(bits & 1) == 2 => false
(bits & 1) == 4 => true
(bits & 1) == 8 => true

but result I am having is that everywhere is false thus rendering all pixels yellow.
Also, originally i wanted to have a text file with bit mask as strings like this

0010
1100
1110
0000

and later to use that as mask but I didn’t know how to convert this to appropriate value, i would need
some help with that as well!

Thanks!

    public void Mark()
    {
        var texture = _target.sharedMaterial.mainTexture as Texture2D;
        
        int bits = 12;

        for (int i = 0; i < _height; i++)
        {
            for (int j = 0; j < _width; j++)
            {
                SetPixel(i, j, (bits & 1) == 1 ? Color.black : Color.yellow, texture);
                SetPixel(i, j, (bits & 2) == 1 ? Color.black : Color.yellow, texture);
                SetPixel(i, j, (bits & 4) == 1 ? Color.black : Color.yellow, texture);
                SetPixel(i, j, (bits & 8) == 1 ? Color.black : Color.yellow, texture);
            }
        }
    }

    private void SetPixel(int row, int column, Color color,Texture2D texture)
    {
        texture.SetPixel(row, column, color);
        texture.Apply();
    }

Try this

SetPixel(i, j, (bits & 1) == 1 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 2) == 2 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 4) == 4 ? Color.black : Color.yellow, texture);
SetPixel(i, j, (bits & 8) == 8 ? Color.black : Color.yellow, texture);

(if you try to find out if the value is in bits)

1100&0001=0000
1100&0010=0000
1100&0100=0100
1100&1000=1000

1100|0001=1101
1100|0010=1110
1100|0100=1100
1100|1000=1100

1100^0001=1101
1100^0010=1110
1100^0100=1000
1100^1000=0100

I fixed the code and still doesn’t work, but i think it’s the logic that is wrong not bit shifts. texture becomes either yellow or black there is no individual pixels set (i was trying to find out the value in bits)

If this is the complete code another problem is that you make the same comparisons in every step of the loop. So I would expect the last line (SetPixel(i, j, (bits & 8) == 8 ? Color.black : Color.yellow, texture) always wins.
Maybe you forgot to change bits in the loop?

All the pixels WILL be the same color, because you are always comparing to the mask ‘12’

Protip: do not texture.Apply() every time, only apply after all pixels have been set, because Apply is expensive

Thanks for looking into this! Here is how I solved it:

    public void Mark(Pattern type)
    {
        var texture = _target.sharedMaterial.mainTexture as Texture2D;

        var pattern = BitPatterns.GetPattern(_width,type);

        for (int i = 0; i < _height; i++)
        {
            for (int j = 0; j < _width; j++)
            {
                if (j == 0) SetPixel(i, j, (pattern[i] & 1) == 1 ? Color.black : Color.yellow, texture);
                if (j == 1) SetPixel(i, j, (pattern[i] & 2) == 2 ? Color.black : Color.yellow, texture);
                if (j == 2) SetPixel(i, j, (pattern[i] & 4) == 4 ? Color.black : Color.yellow, texture);
                if (j == 3) SetPixel(i, j, (pattern[i] & 8) == 8 ? Color.black : Color.yellow, texture);
                if (j == 4) SetPixel(i, j, (pattern[i] & 16) == 16 ? Color.black : Color.yellow, texture);
                if (j == 5) SetPixel(i, j, (pattern[i] & 32) == 32 ? Color.black : Color.yellow, texture);
                if (j == 6) SetPixel(i, j, (pattern[i] & 64) == 64 ? Color.black : Color.yellow, texture);
                if (j == 7) SetPixel(i, j, (pattern[i] & 128) == 128 ? Color.black : Color.yellow, texture);
            }
        }
    }

All those ifs, thats terribly inefficient
why not

int val = Mathf.Pow(2,i)
SetPixel(i, j, (pattern & val) == val ? Color.black : Color.yellow, texture);

Thanks! Great suggestion, i feel a bit embarrassed …:slight_smile: