Bit flipping
April 23, 2016 at 10:30 pm
(This post was last modified: April 23, 2016 at 10:42 pm by SteelCurtain.)
So I am doing my final Discrete Maths project and I am having a little bit of an issue. The prof is really specific, she'll take off for unnecessary code.
I need to do the following without an if or if-else statement. I know it's possible, just need some tricky math.
I need to have one statement which basically does the following:
I have a running total, if that running total is even, then I need to keep a binary bit the same. If that running total is odd, I need to flip the bit.
The following code would work, but I need to do the same without the if-else structure:
The forum does not respect whitespace. Bad Forum!
I need to do the following without an if or if-else statement. I know it's possible, just need some tricky math.
I need to have one statement which basically does the following:
I have a running total, if that running total is even, then I need to keep a binary bit the same. If that running total is odd, I need to flip the bit.
The following code would work, but I need to do the same without the if-else structure:
Code:
for (int i = 1; i < 9; i++) //loop 9 times (the gray code word is 9 bits long)
{
if (grayTot % 2 != 0) //if the running total is not even
binary[i] = gray[i] * -1 + 1; // flip the bit
else //if the running total is even
binary[i] = gray[i]; //keep the bit
grayTot += gray[i]; // update the running total
}
The forum does not respect whitespace. Bad Forum!
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---