Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: March 29, 2024, 12:56 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bit flipping
#1
Bit flipping
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:
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!<---
Reply



Messages In This Thread
Bit flipping - by SteelCurtain - April 23, 2016 at 10:30 pm
RE: Bit flipping - by SteelCurtain - April 23, 2016 at 10:40 pm
RE: Bit flipping - by SteelCurtain - April 23, 2016 at 11:04 pm
RE: Bit flipping - by Jackalope - April 23, 2016 at 11:30 pm
RE: Bit flipping - by SteelCurtain - April 23, 2016 at 11:37 pm
RE: Bit flipping - by Jackalope - April 23, 2016 at 11:45 pm
RE: Bit flipping - by Tiberius - April 23, 2016 at 11:40 pm
RE: Bit flipping - by I_am_not_mafia - April 24, 2016 at 3:22 am
RE: Bit flipping - by SteelCurtain - April 23, 2016 at 11:42 pm
RE: Bit flipping - by Tiberius - April 23, 2016 at 11:55 pm
RE: Bit flipping - by SteelCurtain - April 24, 2016 at 12:01 am
RE: Bit flipping - by SteelCurtain - April 24, 2016 at 12:05 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 1:19 am
RE: Bit flipping - by SteelCurtain - April 24, 2016 at 1:46 am
RE: Bit flipping - by ErGingerbreadMandude - April 24, 2016 at 3:04 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 3:10 am
RE: Bit flipping - by ErGingerbreadMandude - April 24, 2016 at 3:22 am
RE: Bit flipping - by I_am_not_mafia - April 24, 2016 at 3:34 am
RE: Bit flipping - by pocaracas - April 24, 2016 at 3:41 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 4:46 am
RE: Bit flipping - by pocaracas - April 24, 2016 at 5:42 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 7:21 am
RE: Bit flipping - by pocaracas - April 24, 2016 at 7:38 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 7:42 am
RE: Bit flipping - by I_am_not_mafia - April 24, 2016 at 7:28 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 4:57 am
RE: Bit flipping - by I_am_not_mafia - April 24, 2016 at 4:03 am
RE: Bit flipping - by SteelCurtain - April 24, 2016 at 4:10 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 7:25 am
RE: Bit flipping - by Jackalope - April 24, 2016 at 7:41 am
RE: Bit flipping - by Alex K - April 25, 2016 at 8:59 am
RE: Bit flipping - by J a c k - April 25, 2016 at 12:12 am
RE: Bit flipping - by bennyboy - April 25, 2016 at 8:09 am
RE: Bit flipping - by I_am_not_mafia - April 25, 2016 at 8:24 am
RE: Bit flipping - by bennyboy - April 26, 2016 at 4:56 am
RE: Bit flipping - by KevinM1 - April 26, 2016 at 9:52 am
RE: Bit flipping - by pocaracas - April 26, 2016 at 9:55 am
RE: Bit flipping - by SteelCurtain - April 25, 2016 at 8:12 am
RE: Bit flipping - by SteelCurtain - April 25, 2016 at 8:42 am
RE: Bit flipping - by Angrboda - April 25, 2016 at 10:38 am
RE: Bit flipping - by KevinM1 - April 25, 2016 at 10:32 pm
RE: Bit flipping - by bennyboy - April 26, 2016 at 4:48 am

Possibly Related Threads...
Thread Author Replies Views Last Post
  Coin Flipping Poll Tiberius 15 5133 April 19, 2010 at 1:00 pm
Last Post: Violet



Users browsing this thread: 1 Guest(s)