How to add even parity bit on 7-bit binary number

后端 未结 3 988
陌清茗
陌清茗 2021-01-25 11:29

I am continuing from my previous question. I am making a c# program where the user enters a 7-bit binary number and the computer prints out the number with an even parity bit to

3条回答
  •  萌比男神i
    2021-01-25 11:45

    Might be more fun to duplicate the circuit they use to do this..

    bool odd = false;
    
    for(int i=6;i>=0;i--)
      odd ^= (number & (1 << i)) > 0;
    

    Then if you want even parity set bit 7 to odd, odd parity to not odd.

    or

    bool even = true;
    
    for(int i=6;i>=0;i--)
      even ^= (number & (1 << i)) > 0;
    

    The circuit is dual function returns 0 and 1 or 1 and 0, does more than 1 bit at a time as well, but this is a bit light for TPL....

    PS you might want to check the input for < 128 otherwise things are going to go well wrong.

    ooh didn't notice the homework tag, don't use this unless you can explain it.

提交回复
热议问题