[ / / / / / / / / / / / / / ] [ dir / animu / cafechan / eirepol / fa / fast / miku / s8s / ttgg ][Options][ watchlist ]

/prog/ - Programming

Programming board
You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
Password (Randomized for file and post deletion; you may also set your own.)
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

[–]

98fb41 (1)  No.5200>>5201 >>5224 [Watch Thread][Show All Posts]

Boy I sure wish I could do something like if(x != (1 || 2 || 4 || 8 || 16)) instead of if(x != 1 && x != 2 && x != 4 && x != 8 && x != 16). JUST DeMorgancuck my shit right up fam.

Am I retarded and is there a way to actually do this? Or is C# retarded and can't do logic properly?

5c1a8d (2)  No.5201>>5202

>>5200 (OP)

I don't think you can do that, "1 || 2" is asking whether 1 or 2 are true.

One alternative could be to use a switch, but it isn't necessarily better.

[code]switch(x) {

case 1: case 2: case 4: case 8: case 16: break; default:

// something

break;

}[\code]


5c1a8d (2)  No.5202

>>5201

I'm too fucking tired to fix that code block so deal with it.


4ccdca (1)  No.5216>>5217

Python:

if x not in [1, 2, 4, 8, 16]:

stuff

In a good language, you could have just defined some language construct to do what you wanted instead, though.


02fde9 (1)  No.5217

>>5216

Use a set for membership testing, ya turnip.


e7a975 (1)  No.5218

Easy in ruby as well:

Set[1, 2, 4, 8].include? x

And C++:


static const std::unordered_set<int> membership = {1, 2, 4, 8};
if (membership.count(x)) {

Really, any language with some sort of set membership check. C# has HashSet, so you could use that, though it might be slower. I'm not sure how easy C# makes static const variables, so you might incur extra overhead having to construct a set every time if not, or stick the variable into a higher scope.


18a9ee (1)  No.5224

>>5200 (OP)

JavaScript:


if(Math.log2(x)%1){
//...
}




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 5
6 replies | 0 images | 6 UIDs | Page ?
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / animu / cafechan / eirepol / fa / fast / miku / s8s / ttgg ][ watchlist ]