Search⌘ K
AI Features

Solution: Network Permission Flags

Explore how to manipulate network permission flags using bitwise operators in Java. Understand defining flags with powers of two, merging them with the OR operator, and checking specific permissions using the AND operator. This lesson helps you visualize binary representations and apply these techniques effectively in Java programs.

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));
}
}
...