简体   繁体   中英

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, ie AB .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM