7

我正在制作一个马程序。我有马脸,想涂一点面膜。佩戴位掩码时,只能看到马眼。首先,我必须将马脸转换为数字。为此,我有一组位,其中包括用于马脸的 0、0、0 和 1。

我正在使用 C# 并将问题分解为多个部分:

  1. 将马头转换为数字
  2. 为它戴上一个面具
  3. 将位掩码放在马上
  4. 将数字蒙面马转换回图形

在第 4 步,我希望只看到马眼,但我只看到“0”,这甚至不是马脸。

这是我所有的代码,请不要质疑我的 ASCII 艺术它与问题无关,除了它是一个原型,真正的程序将具有更好的图形。

//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";

我怀疑转换有问题,我尝试了各种方法,例如转换为字节数组并用空格格式化字符串,但没有运气。我想知道这个问题是否可能是NP难的。

4

2 回答 2

3

face并且eyesOnly没有共同的 1 位。maskBits留下除了眼睛以外的一切。要么交换01,要么使用~运算符翻转maskBits。并给它一个更好的名称,以便清楚它是什么掩码:bitmaskForNotEyes.

于 2014-06-04T12:09:53.813 回答
2

我认为问题是——

string binaryHead = head.Replace('#', '0').Replace('=', '0').Replace(' ', '0').Replace('O', '1');
  1. 首先,全部'#'更改为'0'.
  2. 然后全部'='改为'0'
  3. 全部' '更改为'0'.
  4. 终于把眼睛'1'

所以,转换后的头部看起来像这样 -

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

现在你正在&使用它 -

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

所以输出很明显0

于 2014-06-04T12:00:11.210 回答