1

I'm writing a program in C++, and it has to be as minimal as possible. I need some help with an if/else statement. This is the code:

if (lines & 1 << ((d & 1) * 30 + 5 * l + c)) {
    cout << "Invalid";
} else {
    lines |= 1 << ((d & 1) * 30 + 5 * l + c);
}

What i'm doing here is: verifing if a bit is set to 1 and I cout something, and if it's not, i'll set it to 1. Is there any way i can combine the 2 lines where i verify and where i set bit to 1 in the if body?

lines & 1 << ((d & 1) * 30 + 5 * l + c)
lines |= 1 << ((d & 1) * 30 + 5 * l + c)

I'm imagining something like:

if (lines |= .... )

And what it should do is: verify if bit is 1 and entering the if body, and if it's not(or it is already) make it 1.

Sorry for my poor english and this stupid request, but i can't figure out a way to do it and it drives me crazy.

P.S: Is there any site/calculator that can give me an boolean formula when i enter an equasion? ex: 3 .. .. = -1. and i need the operand and operator here.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
aleeN1
  • 43
  • 8
  • Why do you want to combine the two lines in the if body? – jpo38 Apr 12 '17 at 18:31
  • What about a ternary operator – Andria Apr 12 '17 at 18:32
  • @jpo38 i want it to be as minimal as posible, when it comes to characters. – aleeN1 Apr 12 '17 at 18:33
  • @chbchb55 i know and have used ternary operators, but i can't figure it out how to use it here, could you help me with the code? – aleeN1 Apr 12 '17 at 18:35
  • Chcek out my answer @aleeN1 – Andria Apr 12 '17 at 18:35
  • Are you allowed to mess with the bit otherwise? For example, can you set the bit to zero in the invalid case? – Mad Physicist Apr 12 '17 at 18:37
  • 1
    @aleeN1: ` i want it to be as minimal as possible` Let the compiler do that if needed – jpo38 Apr 12 '17 at 18:40
  • @MadPhysicist in the invalid case i do nothing to the "lines" variable, but i can make it 1 (it is already 1) so i can unite the 2 code lines into 1. hope you understand what i say. – aleeN1 Apr 12 '17 at 18:43
  • @jpo38. OP means in terms of character count. Think code golf. – Mad Physicist Apr 12 '17 at 18:43
  • As for the site/calculator, it is entirely unclear what you want, and `ex: 3 .. .. = -1.` is totally *not* an example of anything, but to cut a long story short, if [Wolfram|Alpha](https://www.wolframalpha.com/) does not do it, then it is unlikely that anyone else does it. – Mike Nakis Apr 12 '17 at 18:43
  • @aleeN1. I am assuming this is part of a larger code. Can I toggle the bit instead of making it 1? – Mad Physicist Apr 12 '17 at 18:44
  • If you want to reduce the number of lines of code, the best and easiest way is to delete your linefeeds, thus putting all your statements in one line. – Mike Nakis Apr 12 '17 at 18:45
  • @MadPhysicist it is a very short code, actually those lines are the bigger ones, that's why i want to shorten them. No, you can't toggle it, because if it's 1 it remains 1, for further test cases. If the ith bit is 1, it means the ith stick was used. So i can't use that stick for the remaining of the program. – aleeN1 Apr 12 '17 at 18:47
  • the less you type now the harder it will be for you in one month to understand the code. Too much code isnt good but there is a limit where reducing it more mainly hurts readability. Imho you are already far beyond that limit when you have 4 magic numbers in a single line. – 463035818_is_not_an_ai Apr 12 '17 at 18:47
  • @MikeNakis spaces/new lines/tabs are not included in the number of characters. it's just about digits, letters and other signs used in c++. – aleeN1 Apr 12 '17 at 18:48
  • @tobi303 i know that. this is a contest where the one who makes the program working with the least amount of characters wins. it's not for production or anything. it's kinda just 4fun. – aleeN1 Apr 12 '17 at 18:49
  • What are `lines`, `d`, `l`, and `c`'s definitions – Andria Apr 12 '17 at 18:53
  • @aleeN1. You may want to check out https://codegolf.stackexchange.com/ – Mad Physicist Apr 12 '17 at 18:55

1 Answers1

1
if (lines == (lines|= mask)) cout << "Invalid";
  • Isn't this undefined behavior? – Mike Nakis Apr 12 '17 at 18:45
  • 1
    @MikeNakis: why should it ? –  Apr 12 '17 at 18:45
  • 1
    because you are both modifying and reading a variable in the same statement. – Mike Nakis Apr 12 '17 at 18:46
  • I am not saying that I know for sure that this is undefined behavior, but I have the feeling that it is. – Mike Nakis Apr 12 '17 at 18:46
  • I think this is pretty well defined, but won't work because evaluation order is left to right. – Mad Physicist Apr 12 '17 at 18:47
  • @MadPhysicist: absolutely, my bad. –  Apr 12 '17 at 18:48
  • Basically, lines will always be equal to itself after the assignment. I used `^=` instead of `|=` in my answer to get around this. – Mad Physicist Apr 12 '17 at 18:48
  • it is indeed UB. – Jarod42 Apr 12 '17 at 18:49
  • @MadPhysicist: `^=` won't have the same effect, as it will clear the bit when it was set, whereas the original code leaves it unchanged. –  Apr 12 '17 at 18:50
  • Right. OP just clarified that, so I deleted my answer. – Mad Physicist Apr 12 '17 at 18:51
  • It's perfect. I love you guys ;* . – aleeN1 Apr 12 '17 at 18:55
  • 1
    @aleeN1 beware: "undefined behavior" means that it may work in a certain way with your compiler, but with a different compiler, or even with a different version of the same compiler, you might get an entirely different thing, like a bowl of petunias. – Mike Nakis Apr 12 '17 at 18:56
  • @MikeNakis: in the real world, the only alternative is performing the assignment before the comparison, which will always yield a true result. The standard details the evaluation rules http://en.cppreference.com/w/cpp/language/eval_order, which I am totally desperate to successfully check here. –  Apr 12 '17 at 18:59