Cannot apply bit mask
Asked Answered
D

2

7

I am making a horse programme. I have the horse face and wish to apply a bit mask. Only the horses eyes should be visible when it is wearing the bit mask. First I must convert the horses face to digital. For this I have a set of bits which include 0, 0, 0, and 1 for the face of the horse.

I am using C# and have broken the problem into parts:

  1. Convert the horse's head to digital
  2. Build a bit mask for it to wear
  3. Put the bit mask on the horse
  4. Convert the digital masked horse back into graphics

At step 4 I expect only to see the horses eyes but I only see "0" which IS NOT EVEN A HORSE FACE.

Here is all of my code, please don't question my ASCII art it is not relevant to the question, besides it is a prototype the real program will have superior graphics.

//the head of the horse
string head = "#      #" +
              "########" +
              "#O    O#" +
              "#      #" +
              "#      #" +
              "#=    =#" +
              " #====# " +
              "  ####  ";
//digitize the horse into bits of binary
string binaryHead = head.Replace('#', '0').Replace('=', '0').Replace(' ', '0').Replace('O', '1');
long face = Convert.ToInt64(binaryHead, 2);

//make a bit mask with holes for the eyes
string mask = "11111111" +
              "11111111" +
              "10111101" +
              "11111111" +
              "11111111" +
              "11111111" +
              "11111111" +
              "11111111";

//apply the bit mask using C#
long maskBits = Convert.ToInt64(mask, 2);
string eyesOnly = Convert.ToString(face & maskBits, 2);
//eyesOnly is "0"....WHAT??? It should be more than that. WHERE IS THE HORSE??
//It should look like this:
//              "00000000" +
//              "00000000" +
//              "01000010" +
//              "00000000" +
//              "00000000" +
//              "00000000" +
//              "00000000" +
//              "00000000";

I suspect something is wrong with the conversion, I have tried all kinds of things like converting to a byte array and formatting the string with spaces but with no luck. I am wondering if this problem might be NP-hard.

Dullard answered 4/6, 2014 at 11:51 Comment(2)
@Vadim-Kotov you edited out one of the best question titles on SO, shameCarinacarinate
@Carinacarinate Well, maybe. But such things are more appropriate on Meta SO :)Schiro
P
3

face and eyesOnly have no common 1-bits. maskBits leaves everything except for the eyes. Either swap 0 and 1, or use the ~ operator to flip maskBits. And give it a better name so that it is clear what it is a mask for: bitmaskForNotEyes.

Penneypenni answered 4/6, 2014 at 12:9 Comment(0)
W
2

I think the problem is -

string binaryHead = head.Replace('#', '0').Replace('=', '0').Replace(' ', '0').Replace('O', '1');
  1. First, all '#' are changed to '0'.
  2. Then all '=' are changed to '0'
  3. All ' ' are changed to '0'.
  4. Finally the eyes to '1'

So, after the conversion the head looks like this -

string head = "00000000" +
              "00000000" +
              "01000010" +
              "00000000" +
              "00000000" +
              "00000000" +
              " 000000 " +
              "  0000  ";

Now you are doing & with it -

string mask = "11111111" +
              "11111111" +
              "10111101" +
              "11111111" +
              "11111111" +
              "11111111" +
              "11111111" +
              "11111111";

so the output is obviously 0.

Waxwing answered 4/6, 2014 at 12:0 Comment(1)
the spaces are '0' in the head string.Midstream

© 2022 - 2024 — McMap. All rights reserved.