Multiple boolean flags or masking int?

Multiple boolean flags or masking int?

Other urls found in this thread:

docs.oracle.com/javase/tutorial/java/javaOO/enum.html
stackoverflow.com/questions/6282619/is-there-a-way-to-bitwise-or-enums-in-java
docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html
eddmann.com/posts/using-bit-flags-and-enumsets-in-java/
godbolt.org/g/PT8c74
godbolt.org/g/UhnVjT
fuseyism.com/classpath/doc/java/util/EnumSet-source.html
twitter.com/NSFWRedditVideo

Struct with bitfields?

public class Setting {
private boolean flag1;
private boolean flag2;
// ...
public int getFlag1() {
return this.flag1;
}
public void setFlag1(boolean newFlag) {
this.flag1 = newFlag;
}
// ...
}

Do compilers collaspe multiple booleans into ints where bits represent booleans?

bools are a byte in java

yes

>multipleBooleanFlags || maskingInt
true

If we're talking C++, technically the size of bool is undefined, but if it needs to be addressable it's probably no less than 1 byte.

Might be more depending on CPU arch.

>private fields with getters and setters that do nothing
Nice anti-pattern.

>that do nothing
nigger what

Nice bait

They do nothing beyond setting the field.
Just make the field public.
t. dumb java college kid

T. Cs undergrad

find all of your variables and store the flags in the sign bit

Give me a valid reason to use getters and setters over public fields for that class.

tell me why you would define flags as private if you have a public function to change them.

inb4 softwar engineerin' tactics

to keep your job

the real reason is ensuring the ability to change the logic of those "fields"

another good reason is future validation, about when you can change something, what you can change it to, etc

also, with respect to validation and checking, you might also want to do locking for setting a field

sure, you don't do any of that in the example, but if you ever wanted to do any of that but used fields directly, you would have to refactor that on top of adding your code change. why not just do it from the start, seeing as it's super easy to implement.

if you don't like writing them explicitly, use lombok, which is a fantastic tool

docs.oracle.com/javase/tutorial/java/javaOO/enum.html

If begins Java.

Neither. Enum

Enum seems like a different use case to me.
To represent n boolean flags as an enum you would 2^n enum values. Doesn't scale well.

What?

Use class EnumSet
stackoverflow.com/questions/6282619/is-there-a-way-to-bitwise-or-enums-in-java

docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html

An Enum allows a variable to represent some number of predefined states.
A set of boolean flags represents 2^n states.
I don't see this solving the problem.
If a I have a boolean flag for each field representing if it's been set, this takes either n booleans, an int (n < 32), or 2^n enums.

Oh, i understand now. For a single instance implementation i would use boolean flags. For more than single instance an enum class is pretty good.

eddmann.com/posts/using-bit-flags-and-enumsets-in-java/

Float array ID returned from OpenCL

multiple boolean flags is easier to understand and relies on less fuckery

What fuckery?

Many people consider simple bitwise operators like some sort of black magic. They simply just can't get their head around them

Hence the 'fuckery'

i do this in VB because some fuck decided that custom types cannot be public members of classes

>VB
how haven't you sudoku'd yet

You can just use multiple flags for readability. Any decent compiler is going to crunch those down to bitwise masks anyway.

>Just make the field public.
You've really activated my almonds that control my "shitting on the new intern during code review" neuron

>public fields are always bad!
>basic public getter/setters are okay though
Why do people fall for this meme?
If you're writing a class that is going to be used by lots of people and/or you can reasonably expect you'll need to add additional behavior on get/set in the future then do getters/setters. A glorified struct class for holding some boolean flags does not qualify.

If you have 5 settings you need to apply, you or 5 unums together when you set it.
It is readable and it works well with "apply all these settings" scenarios.
When they are exclusive, you only apply one.
What scenario would you not want a boolean, but you want a bitmask?

std::vector or std::bitset

Using Java for example I have a builder pattern object with 15 fields that can be set. Based on the value of field1 there are k possible states that are valid each corresponding to some subset of the fields being set.
Using boolean flags I would have 15 booleans each being set when the corresponding field is set, then based on field1 I would AND some subset of these flags to verify the mandatory fields have been set.
Using bitflags I would create an int flags and when each field is set I would OR flags with a power of 2. Then based on field1 I would AND flags with some bitmask corresponding to that state.
The benefit being I can use a Short or an Int versus 15 booleans each taking up a byte. I believe an enum solution will be close to the same as using booleans in Java at least.

I'm afraid not.

godbolt.org/g/PT8c74

The enum just makes it readable.
Instead of and/or magic numbers, you use the enum.
If you have 15 options, you need to list those options somewhere and an enum makes a lot of sense here.

I don't see how enum is any more readable than boolean flags.
And bitflags aren't that bad if you just put a comment by the mask(s).

Meanwhile, in glorious C++ zero cost abstraction land:

godbolt.org/g/UhnVjT

A comment by the mask means you have to do that every time you use it.
The enum is declared with the function.
It is just a slightly better define.

because in OPs example if flag1 was for turning a ship left, and you wanted to warnOtherShipsOfTurn();, you'd have to call that everytime you set the boolean. You'd also have to rewrite the code base which could cost thousands if you're in a large code base.

however if you didn't have coding skills of a wordpress indian you'd know this

You'd only need to comment the definition.

That's not a realistic example.
Besides turning should encapsulated and not be represented by a setter.

Just clear and set your bool bits in a byte and dont fuck up when you code. If you doubt your programming skills make a get and a set function that takes whatever enum you have defined your bool to

comfy af

Can second std::bitset. std::vector isn't really needed in this case.

Fuck off, Stallman. Non-standard extensions are cancer.

Bit fields aren't extensions retard.

Bitfields are standard C though.

fuseyism.com/classpath/doc/java/util/EnumSet-source.html