Skip to content
Justice Tech School

Bitwise Operators and Ternary

& | ^ ~ << >> operate on bits. The ternary condition ? a : b is a short if. instanceof tests types and ++ -- change by one.

Example
System.out.println((6 & 3) + " " + (6 | 3) + " " + (6 ^ 3) + " " + (1 << 4) + " " + (16 >> 2));
int age = 20;
System.out.println(age >= 18 ? "adult" : "minor");
Object o = "text";
System.out.println(o instanceof String);

Result

2 7 5 16 4
adult
true

Exercise

Shift left.

1  3

Quiz Test

What does 1 << 3 give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.