Using the bitwise operators : Binary Bit « Language Basics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Language Basics » Binary Bit 




Using the bitwise operators
Using the bitwise operators
  
//: c03:BitManipulation.java
// Using the bitwise operators.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.*;

public class BitManipulation {

  public static void main(String[] args) {
    Random rand = new Random();
    int i = rand.nextInt();
    int j = rand.nextInt();
    printBinaryInt("-1", -1);
    printBinaryInt("+1", +1);
    int maxpos = 2147483647;
    printBinaryInt("maxpos", maxpos);
    int maxneg = -2147483648;
    printBinaryInt("maxneg", maxneg);
    printBinaryInt("i", i);
    printBinaryInt("~i", ~i);
    printBinaryInt("-i", -i);
    printBinaryInt("j", j);
    printBinaryInt("i & j", i & j);
    printBinaryInt("i | j", i | j);
    printBinaryInt("i ^ j", i ^ j);
    printBinaryInt("i << 5", i << 5);
    printBinaryInt("i >> 5", i >> 5);
    printBinaryInt("(~i) >> 5"(~i>> 5);
    printBinaryInt("i >>> 5", i >>> 5);
    printBinaryInt("(~i) >>> 5"(~i>>> 5);

    long l = rand.nextLong();
    long m = rand.nextLong();
    printBinaryLong("-1L", -1L);
    printBinaryLong("+1L", +1L);
    long ll = 9223372036854775807L;
    printBinaryLong("maxpos", ll);
    long lln = -9223372036854775808L;
    printBinaryLong("maxneg", lln);
    printBinaryLong("l", l);
    printBinaryLong("~l", ~l);
    printBinaryLong("-l", -l);
    printBinaryLong("m", m);
    printBinaryLong("l & m", l & m);
    printBinaryLong("l | m", l | m);
    printBinaryLong("l ^ m", l ^ m);
    printBinaryLong("l << 5", l << 5);
    printBinaryLong("l >> 5", l >> 5);
    printBinaryLong("(~l) >> 5"(~l>> 5);
    printBinaryLong("l >>> 5", l >>> 5);
    printBinaryLong("(~l) >>> 5"(~l>>> 5);
  }
  static void printBinaryInt(String s, int i) {
    System.out.println(
      s + ", int: " + i + ", binary: ");
    System.out.print("   ");
    for(int j = 31; j >= 0; j--)
      if(((<< j&  i!= 0)
        System.out.print("1");
      else
        System.out.print("0");
    System.out.println();
  }
  static void printBinaryLong(String s, long l) {
    System.out.println(
      s + ", long: " + l + ", binary: ");
    System.out.print("   ");
    for(int i = 63; i >= 0; i--)
      if(((1L << i& l!= 0)
        System.out.print("1");
      else
        System.out.print("0");
    System.out.println();
  }
///:~



           
         
    
  














Related examples in the same category
1. Utility for byte swapping of all java data types
2.Bitwise DemoBitwise Demo
3.Binary Digits
4.Bitwise complement (~): inverts ones and zeroes in a number
5.Convert a number to negative and back
6.Bitwise AND (&)
7.Bitwise OR (|)
8.Bitwise XOR (^)
9.Left shift (<<)
10.Performing Bitwise Operations on a Bit Vector
11.Converting Between a BitSet and a Byte Array
12.Returns a byte array of at least length 1
13.Shift to the left
14.Signed shift to the right
15.Unsigned shift to the right
16.Bit-level unpacking of floating-point data
17.Gets the a single bit of the target.
18.Returns 16 bits from the long number.
19.Sets a specific bit of an int.
20.Fuses the lower 16 bits of two ints.
21.Class to represent unsigned 32-bit numbers.
22.A list of bits.
23.Gets the specified bit (0-31) from the integer argument.
24.Sets the specified bit (0-31) in the integer argument.
25.Clears a range of bits in the specified integer.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.