Skip to content

Instantly share code, notes, and snippets.

@1Caxz
Last active December 22, 2022 08:32
Show Gist options
  • Save 1Caxz/a3bb6b60fd8fe7111a2dd6bffa430c05 to your computer and use it in GitHub Desktop.
Save 1Caxz/a3bb6b60fd8fe7111a2dd6bffa430c05 to your computer and use it in GitHub Desktop.

Revisions

  1. 1Caxz revised this gist Dec 22, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bitwise-example-java
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,12 @@ public class Main {
    System.out.print(" " + Integer.parseInt("00001000", 2)); // = 00001000
    System.out.println(" " + (12 & 25));

    // Bitwise XOR (1 & 0 = 1) - (1 & 1 = 0)
    System.out.print("\nAND: " + Integer.toString(12, 2)); // 00001100
    System.out.print(" " + Integer.toString(25, 2)); // ^ 00011001
    System.out.print(" " + Integer.parseInt("00010101", 2)); // = 00010101
    System.out.println(" " + (12 ^ 25));

    // Bitwise Complement (1 -> 0) - (0 -> 1), ~n = -(n + 1)
    System.out.print("\nComp: " + Integer.toString(35, 2)); // 00100011
    System.out.print(" " + Integer.parseInt("11011100", 2)); // = 11011100 -> result 220. why? its because binary also equal to -36
  2. 1Caxz created this gist Dec 22, 2022.
    38 changes: 38 additions & 0 deletions bitwise-example-java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ```java
    public class Main {

    public static void main(String[] args) {
    // Bitwise OR (1 | 0 = 1)
    System.out.print("OR: " + Integer.toString(12, 2)); // 00001100
    System.out.print(" " + Integer.toString(25, 2)); // | 00011001
    System.out.print(" " + Integer.parseInt("00011101", 2)); // = 00011101
    System.out.println(" " + (12 | 25));

    // Bitwise AND (1 & 0 = 0) - (1 & 1 = 1)
    System.out.print("\nAND: " + Integer.toString(12, 2)); // 00001100
    System.out.print(" " + Integer.toString(25, 2)); // & 00011001
    System.out.print(" " + Integer.parseInt("00001000", 2)); // = 00001000
    System.out.println(" " + (12 & 25));

    // Bitwise Complement (1 -> 0) - (0 -> 1), ~n = -(n + 1)
    System.out.print("\nComp: " + Integer.toString(35, 2)); // 00100011
    System.out.print(" " + Integer.parseInt("11011100", 2)); // = 11011100 -> result 220. why? its because binary also equal to -36
    System.out.println(" " + (~35));

    // Bitwise Left Shift - bit move to left
    System.out.print("\nLS: " + Integer.toString(35, 2)); // 00100011
    System.out.print(" " + Integer.parseInt("01000110", 2)); // = 01000110
    System.out.println(" " + (35 << 1));

    // Bitwise Signed Right Shift - bit move to right
    System.out.print("\nSRS: " + Integer.toString(35, 2)); // 00100011
    System.out.print(" " + Integer.parseInt("00010001", 2)); // = 00010001
    System.out.println(" " + (35 >> 1));

    // Bitwise Unigned Right Shift - bit move to right, leftmost position is filled with 0 instead of the sign bit.
    System.out.print("\nURS: " + Integer.toString(35, 2)); // 00100011
    System.out.print(" " + Integer.parseInt("00010001", 2)); // = 00010001
    System.out.println(" " + (35 >>> 1));
    }
    }
    ```