#Can I ensure that a variable will contain just one value using bitwise and without if statements?

1 messages · Page 1 of 1 (latest)

alpine lantern
#

I'm beeing introduced to bitwise operators and I know that it performs a certain operation in each bit of a variable.

Can I ensure that this variable will contain only the value 3 using any bitwise operation?

// if i==3: ((0) + 3) & 3 == 0b11 & 0b11 == 0b11
int mod3 = ((i % 3) + 3) & 3;

Testing:

int mod3 = (5) & 3              //0b101 & 0b011 == 0b001 -> mod3 == 1?
noble oasisBOT
#

<@&987246883653156906> please have a look, thanks.

noble oasisBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

alpine lantern
#

Yes, presuming that i==3 in this case

viral quail
#

I don't understand your question

alpine lantern
#

I'm asking if I could make a variable to only contain one single value. Like, if I want it to only contain the value 3, I would do (if possible) a bitwise operation in order to ensure that.

Example

int i shouldBeThreeOrZero = ((i % 3) + 3) & 3;
#

I think I'll try to search for it, it's hard to ask

viral quail
#

Why can't you just do I == 3

alpine lantern
#

Because that's a boolean operation and would return 1 or 0, witch cannot be stored in a integer

#

And I cannot use if's or ternary if's

viral quail
#

Also, if you want 0 if it is a 3, otherwise any number:

int isThree = i - 3;

If you really want that is 0 if it is a 3, but 1 if it isn't:

int isThree = i - 3 & 1;
alpine lantern
#

This is the piece of code. FizzBuzz without direct if's (Boolean.compare.... has if's I know)

#

I know it's not the most optimal solution, but I haven't searched anything yet, so give it a credit

#

I'm having problems because not all values in the output are correct

#

Some of them are

viral quail
#

Why are you doing i % 3 +3

#

If I is 6, it is true

#

Just do a subtraction

alpine lantern
#

only [3] : "Fizz"
only [5]: "Buzz"
both [3+5]: "FizzBuzz"

alpine lantern
inland prism
alpine lantern
#

But there still are indirect if's in some methods I've called

flat gust
#

@alpine lantern i took a shot at it if you want to see

inland prism
#

It's possible to fit a FizzBuzz within a for loop, even without a body

#

You'd use ternary expressions

alpine lantern
flat gust
#
public class Main {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            interface Fizzer {
                void fizz(int i);
            }


            var fizzers = new Fizzer[] {
                    (__) -> System.out.println("fizzbuzz"),
                    (__) -> System.out.println("fizz"),
                    (__) -> System.out.println("buzz"),
                    System.out::println
            };
            var fizzups = new int[] {0, 1, 1};
            var fizzies = new int[] {0, 2, 2, 2, 2};

            fizzers[fizzups[i % 3] + fizzies[i % 5]].fizz(i);
        }
    }
}
#

@alpine lantern There is still an if for the for loop

#

i have a few ideas for how to get rid of that

inland prism
alpine lantern
#

I'll need minutes to understand that, I've not studied yet lambdas, method references and some stuff

#

var fizzups = new int[] {0, 1, 1};
var fizzies = new int[] {0, 2, 2, 2, 2};
Why is this?

flat gust
#

look at the first arra

#

it indexes the possible results of i % 3 to a number

#

1 + 2 means neither i % 3 or i % 5 match

alpine lantern
#

Why the number 2 isn't there?

Shoudn't k % 3 produce results [0..2]?

flat gust
#

nonono

#

the index is [0..2]

#

just step through it

alpine lantern
#

Oh, okay

#

I'll need some time to read and understand it all

#

Thanks

#

Oh, and where is the fizz() method?

#
(__) -> System.out.println("fizzbuzz"),
(__) -> System.out.println("fizz"),
(__) -> System.out.println("buzz"),

are this functions its definition?

#

I think I got it

viral quail
flat gust
#

could have changed the interface to return a string and gotten rid of the duplicate printlns tho

viral quail
# flat gust could have changed the interface to return a string and gotten rid of the duplic...

Ah yes, something like that

public class Main {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            interface Fizzer {
                String fizz(int i);
            }


            var fizzers = new Fizzer[] {
                    _ -> System.out.println("fizzbuzz"),
                    _ -> System.out.println("fizz"),
                    _ -> System.out.println("buzz"),
                    String::valueOf
            };
            var fizzups = new int[] {0, 1, 1};
            var fizzies = new int[] {0, 2, 2, 2, 2};

            System.out.println(fizzers[fizzups[i % 3] + fizzies[i % 5]].fizz(i));
        }
    }
}
flat gust
#

you need two underscores for today's java + the three lambdas need to be changed

#

(for Arthur, since he's reading)

alpine lantern
#

I did not understand quite well these:

            var fizzups = new int[] {0, 1, 1};
            var fizzies = new int[] {0, 2, 2, 2, 2};

I know fizzups goes [0..2] because % 3 and I know fizzies goess [0..4] because % 5

But why the number 1 in the first arr and the number 2 in the second arr? Is it because we need to access the fizzers array (0..3)? How did you think about this?

#

fizzups[0] and fizzies[0] means that i is divisible by both 3 and 5. and "fizzbuzz" is at the first index of fizzers because of that, what I did not understand was:

Where did the 1's and 2's come from?

alpine lantern
#

my solution

#

With bg: