Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot apply bit mask

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.

like image 248
Weyland Yutani Avatar asked Jun 04 '14 11:06

Weyland Yutani


1 Answers

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.

like image 102
usr Avatar answered Sep 29 '22 22:09

usr