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: November 27, 2024, 6:03 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
#2
RE: Bit flipping
Just so you can get an idea of what I'm doing:

Prompt Wrote:The Computer Science Central Intelligence Agency (CSCIA) employs super paranoid computer spies. In order to communicate with each other, they encode their messages. But they aren’t satisfied with a simple encoding of the messages. Messages are made up of ASCII characters. The binary number for the ASCII character is converted to Excess-127 code. The Excess-127 code is treated as a binary code word and is converted to Gray code, then the Gray code word is encoded using a Hamming code because the transmission lines are noisy. Since ASCII characters are 8 bits, the excess-127 code (and therefore the gray code) could be 9 bits long. This means that the hamming code words will be 13 bits long. Your job is to write the decode procedure. Messages will be received as 13 bit packets (you will need to read from the file, packets.dat that has one packet per line). THERE ARE NO SPACES BETWEEN THE BITS! Your program should read a packet, determine if an error has occurred in transmission, fix any error, extract the gray code message, convert to binary code message, convert the binary to decimal (but remember this is excess 127, so after converting to decimal, you need to take care of that) and print the character. Once the decimal value is determined, you must print the character using ONLY a cout statement (that means no if’s or loops or switches, etc.). Then repeat this procedure for each packet in the file. Your output should contain ONLY the decoded characters plus a single cout <<endl; at the very end of the program. Note: My solution is 61 lines of (non-{}) code, and there was only one if statement that I used to see if there was an error in the hamming code. NO else's, no else-if's, no switches, no ?: operators... LOTS of mod arithmetic, though. :-)

Two sample packets.dat files are shown, but do not download the files. Copy and paste the contents into your data files. Otherwise you get garbage. Huge note: You have no idea what the size will be of my test data file, so you HAVE to read to end of file and do it in the proper manner or you will create an error that is really easy for me to catch.


The code above is converting the gray code to a binary code.
"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
#3
RE: Bit flipping
Nevermind y'all are too slow!

Code:
binary[i] = abs(((grayTot % 2) * -1) + gray[i]);
"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
#4
RE: Bit flipping
No ?: operators? How uncivilized.

You really should have some variation on !(total && 1) in there someplace. Your instructor should have forbade function calls as well. Tongue
Reply
#5
RE: Bit flipping
She did forbid all of that, in class. Standard library functions are allowed. abs() and pow() and whatnot.

My code ended up actually shorter than hers... Without my name/course/filename comment block at the beginning and not counting {}'s I am at 54 lines of code.

Efficiency!
"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
#6
RE: Bit flipping
What was her reasoning for making the code so efficient? I mean, it's great to write efficient code, but that's usually something you do in advanced programming classes rather than a discrete math class.
Reply
#7
RE: Bit flipping
(April 23, 2016 at 11:40 pm)Tiberius Wrote: What was her reasoning for making the code so efficient? I mean, it's great to write efficient code, but that's usually something you do in advanced programming classes rather than a discrete math class.

She's the department head. Preparation, I guess.
"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
#8
RE: Bit flipping
(April 23, 2016 at 11:37 pm)SteelCurtain Wrote: She did forbid all of that, in class. Standard library functions are allowed. abs() and pow() and whatnot.

I wouldn't have allowed that - I'd force you to do it with operators only. Mwhahahahaha....

(April 23, 2016 at 11:37 pm)SteelCurtain Wrote: My code ended up actually shorter than hers... Without my name/course/filename comment block at the beginning and not counting {}'s I am at 54 lines of code.

Efficiency!

True - but there's no point in optimizing that code. Tongue
Reply
#9
RE: Bit flipping
(April 23, 2016 at 11:42 pm)SteelCurtain Wrote:
(April 23, 2016 at 11:40 pm)Tiberius Wrote: What was her reasoning for making the code so efficient? I mean, it's great to write efficient code, but that's usually something you do in advanced programming classes rather than a discrete math class.

She's the department head. Preparation, I guess.

Honestly, it seems to me like she was just trying to be difficult. Like Cthulhu said, there's no point in optimizing that code. Using if statements are just as valid a way of writing it, and in fact might be the more preferable way of writing it since it's more obvious to people what the code does.
Reply
#10
RE: Bit flipping
Operators are technically standard library functions anyways, though. Why not other math functions like absolute value or pow? (since for some stupid reason c++ doesn't have an exponent operator)

Of course there isn't a real reason to be this efficient with this code. Other than trying to think in that mindset when coding in the first place. I think she wants to separate the wheat from the chaff, though.

This assignment could have taken 20 mins. The basic algorithm was simple as long as you knew what Hamming Codes, Gray Codes, Excess-127, etc was. It the added constraints that were the difficult part.

I suppose that was the way to make this a "final project" of sorts.
"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



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



Users browsing this thread: 2 Guest(s)