Let's say I have a byte:
byte myByte = 0;
This is what the bits look like in my byte:
0 0 0 0 0 0 0 0
Now I'm going to set my byte to 1, which looks like this:
0 0 0 0 0 0 0 1
Or 3:
0 0 0 0 0 0 1 1
Or 7:
0 0 0 0 0 1 1 1
What I am trying to figure out is how to set specific bits in my byte. For example:
byte myByte = 0;
// 0 0 0 0 0 0 0 0
if(a_first_statement){
// set the first bit to `1`
// 0 0 0 0 0 0 0 1
}
if(a_second_statement){
// set the second bit to `1`
// 0 0 0 0 0 0 1 0
}
if(a_third_statement){
// set the third bit to `1`
// 0 0 0 0 0 1 0 0
}
I would like to do this for these cases:
a_first_statement-> 0 0 0 0 0 0 0 1 // int 1
a_second_statement -> 0 0 0 0 0 0 1 0 // int 2
a_first_statement && a_second_statement -> 0 0 0 0 0 0 1 1 // int 3
a_third_statement -> 0 0 0 0 0 1 0 0 // int 4
a_first_statement && a_third_statement -> 0 0 0 0 0 1 0 1 // int 5
a_second_statement && a_third_statement -> 0 0 0 0 0 1 1 0 // int 6
a_first_statement && a_second_statement && a_third_statement -> 0 0 0 0 0 1 1 1 // int 7
I have tried different iterations of &
, |
, <<
, and >>
byte myByte = 0;
System.out.println(myByte); // prints 0
System.out.println(myByte & 1); // prints 0
System.out.println(myByte | 1); // prints 1
System.out.println(myByte << 1); // prints 0
System.out.println(myByte >> 1); // prints 0
But nothing seems to be giving me what I want.
EDIT: I know this post has been marked as a duplicate by "Jacob G", but the link he provided didn't address my specific situation. I found those answers in my search, but I, obviously, didn't understand them enough to apply to my situation.