Advertisement
Phr0zen_Penguin

Bitwise AND 1 + The Modulus Operator

Dec 18th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [BitwiseAND1.java]
  4.   A thorough probe of the binary abyss: exploiting the logic that 'x' ANDed with 1 WILL ALWAYS
  5.   yield a result of either 1, if 'x' is an odd number (having its lowest order bit set to 1),
  6.   or 0, if 'x' is an even number (having its lowest order bit set to 0)...
  7.  
  8.   ...and the symbolic link between the aforementioned operation and: x % 2.
  9.  
  10.         (c) Damion 'Phr0z3n.dev' Tapper, 2014.
  11.  
  12.         Email: Phr0z3n.Dev@Gmail.com
  13.         Website: http://L337Stuff.Blogspot.com
  14.   --------------------------------------------------------------------------------------------------
  15.   ==================================================================================================*/
  16. public class BitwiseAND1
  17. {
  18.     public static void main(String arg[])
  19.     {
  20.         new BitwiseAND1();
  21.     }
  22.  
  23.     BitwiseAND1()
  24.     {
  25.         for(int i = 0; i <= 10; i++)
  26.         {
  27.             System.out.print(i + " is an ");
  28.  
  29.                 System.out.print( (i % 2 == 0) /* i AND 0 */ ? "even" : "odd" );
  30.  
  31.             System.out.print(" number.  " + i + " AND (binay) 1 MUST be ");
  32.  
  33.                 System.out.print( (i % 2 == 0) ? "0" : "1" ); // The new and improved conditional operator.
  34.  
  35.             System.out.println(": ");
  36.             System.out.println(i + " & 1 = " + (i & 1));
  37.             System.out.println(); // POSIX-friendly (\n is too selfish)
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement