开发者

Operations on bytes in C#

I'm writing application to control LEDS on LPT. I have everything working except this. This is one small function.

I have sth like that:

I want to build function that will take two argument and return one number: In actual code those binary numers will be in hex. I put them there like that so that it's easier for you to visualize it.

Example1:

arg1 = 1100 1100
arg2 = 1001 0001
retu = 0100 1100

Example2:

arg1 = 1111 1111
arg2 = 0001 0010
retu = 1110 1101

Example3:

arg1 = 1111 0000
arg2 = 0010 0010
retu = 1101 0000

I have no idea how this function should 开发者_开发问答look like. I want it to be as fast as possible.

I'll call this function 200 times per second.


Essentially, the set bits in the second argument are those you want removed. So you can simply and with the negated second argument:

byte Foo(byte a, byte b) {
  return (byte)(a & ~b);
}

Your examples at least follow this.

As Alexandre C. notes in a comment to the question, the function is called an implication, i.e. AB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜