Search⌘ K
AI Features

Solution: Network Permission Flags

Explore how to define and manipulate network permission flags using bitwise operators in Java. Understand setting unique bits with powers of two, merging flags with the OR operator, and performing checks with the AND operator. This lesson helps you visually confirm active permission bits for effective flag handling.

We'll cover the following...
Java 25
public class PermissionManager {
public static void main(String[] args) {
// 1. Define distinct bit flags
int READ = 1; // Binary: 001
int WRITE = 2; // Binary: 010
int EXECUTE = 4; // Binary: 100
// 2. Combine flags using Bitwise OR (|)
// 001 | 010 = 011 (Decimal 3)
int userPerms = READ | WRITE;
// 3. Check for specific permissions using Bitwise AND (&)
// (011 & 100) == 100 ? -> 000 == 100 ? -> false
boolean canExecute = (userPerms & EXECUTE) == EXECUTE;
// (011 & 001) == 001 ? -> 001 == 001 ? -> true
boolean canRead = (userPerms & READ) == READ;
// 4. Output boolean checks
System.out.println("Can Execute: " + canExecute);
System.out.println("Can Read: " + canRead);
// 5. Use the Wrapper method to inspect the bits
System.out.println("Binary Perms: " + Integer.toBinaryString(userPerms));
}
}
...