Jump to content
  • Advertisement
Sign in to follow this  
JohnsonGPS

Please help me to understand this line

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am reading a C++ program, and the following line really gives me a headache. Does anybody know something about this type of assignment? To me the bitwise operator is really abused here. const int MAX_PACKETS = 1 | 30 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 10 | 10 | 1 | 16 | CONFIG_MAX; //what is this for? and const int MAX_TEST = 1 | 30 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 16; //What is its value? Thanks in advance. Johnson

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Kalasjniekof
LOL? Anyway, try running those through a simple console program and cout the value ;)


then slap the person who wrote that program...

all the |1's, as can other repeats, can be copacted into one of them, they read like so

const int MAX_PACKETS = 1 | 30 | 10 | 16 | CONFIG_MAX;



const int MAX_TEST = 1 | 30 | 16;

Share this post


Link to post
Share on other sites
I don't think the person that wrote those snippets knew what he was doing.

Compare the output from those snippets with these.

const int MAX_PACKETS = 1 | 30 | CONFIG_MAX;

const int MAX_TEST = 1 | 30;

Share this post


Link to post
Share on other sites
This looks like somebody was trying to be clever.

Quote:
const int MAX_TEST = 1 | 30 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 16; //What is its value?


I'm guessing there are 12 tests, the first is of size 1, the second of size 30, the third 1, and so on. By bitwise-or'ing them all together you get a (loose) upper bound on the maximum size of any one test.

I wouldn't recommend copying this example. It's hard to understand and maintain. At the very least there needs to be a comment explaining why they're doing something so wierd.

Share this post


Link to post
Share on other sites
Thanks for your help.

I still could not understand why that guy made this joke on the program. There are lots of easier and clearer ways to do the same task.

Johnson

Share this post


Link to post
Share on other sites
Wow, thats bad code. Well, the first thing to do to make sense of it is to realizes that 1 | 1 == 1. Now, if it were 16, 32, 64, I'd say it was boolean operators being used as bitmasks, but 10? 30? No, thats not understanding how bits work.

10|1==11
30|1==31
10|30==30
1|10|30==31

So, what we have here is code that doesn't make sense.

Share this post


Link to post
Share on other sites
Maybe the ones are intended as placeholders for other values to be added later? Somebody heard that you need to make code that can easily be extended, so he made room for other tests.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
This looks like somebody was trying to be clever.

Quote:
const int MAX_TEST = 1 | 30 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 16; //What is its value?

By bitwise-or'ing them all together you get a (loose) upper bound on the maximum size of any one test.


I think you hit the nail on the head.

$5 says that it would make a little more sense if the usage of MAX_TEST and the other var is shown. Each of the values between the bitwise-or's most likely represents ... something .. that needs to be tested. By bitwise or'ing the test count (??) of each of the .. somethings .. you get a very loose upperbound. Probably the programmer's idea of easily manipulating the upperbound without going through some sort of loop to find the max of an abritrary number of .. somethings ..

Either way, someone needs to be punched in the gut for that :) hehehe

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!