Advertisement
vencinachev

BitwiseMasks-Java

Mar 26th, 2021
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1.  
  2. public class Program {
  3.     public static boolean containsBit(int mask, int el) {
  4.         return ((mask & (1 << el)) != 0) ? true : false;
  5.     }
  6.    
  7.     public static int insertBit(int mask, int el) {
  8.         return (mask | (1 << el));
  9.     }
  10.    
  11.     public static int eraseBit(int mask, int el) {
  12.         return (mask & ~(1 << el));
  13.     }
  14.    
  15.     public static int changeBit(int mask, int el) {
  16.         return (mask ^ (1 << el));
  17.     }
  18.    
  19.     public static void main(String[] args) {
  20.         int mask = 0;
  21.         mask = insertBit(mask, 3);
  22.         mask = insertBit(mask, 5);
  23.         mask = insertBit(mask, 10);
  24.         mask = eraseBit(mask, 5);
  25.         mask = changeBit(mask, 3);
  26.         mask = changeBit(mask, 0);
  27.         for (int i = 0; i < 32; i++) {
  28.             System.out.println("Bit[" + i + "] = " + containsBit(mask, i));
  29.         }
  30.     }
  31.  
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement