int bit operations

In this chapter you will learn:

  1. How to reverse and rotate the integer in binary format
  2. How to bit oriented operation on int type value
  3. How to get the leading and trailing zeros

Reverse and rotate the integer in binary format

  • static int reverse(int i) reverses the bits.
  • static int reverseBytes(int i) reverses the bytes.
  • static int rotateLeft(int i, int distance) rotates the bits by distance to left.
  • static int rotateRight(int i, int distance) rotates the bits by distance to right.

The following code reverse decimal value 10 in binary format.

public class Main {
  public static void main(String[] args) {
/*  j a  va 2  s  . c om*/
    System.out.println(Integer.reverse(10));

  }
}

The output:

The following code rotates value 10 to left for distance of 2.

public class Main {
  public static void main(String[] args) {
//  j a  v  a  2s . c o  m
    System.out.println(Integer.rotateLeft(10,2));

  }
}

The output:

Bit oriented operation

  • static int bitCount(int i) counts the bits
  • static int highestOneBit(int i) returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit.
  • static int lowestOneBit(int i) returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit.

The following code does the bit oriented operation on value 10.

public class Main {
  public static void main(String[] args) {
//from   j a  va 2  s .  c o m
    System.out.println(Integer.bitCount(10));
    System.out.println(Integer.highestOneBit(10));
    System.out.println(Integer.lowestOneBit(10));
  }
}

The output:

Get the leading and trailing zeros

  • static int numberOfLeadingZeros(int i) returns the number of zero bits preceding the highest-order ("leftmost") one-bit
  • static int numberOfTrailingZeros(int i) returns the number of zero bits following the lowest-order ("rightmost") one-bit
public class Main {
  public static void main(String[] args) {
//from   j  a v a  2 s .  co  m
    System.out.println(Integer.numberOfLeadingZeros(10));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Java long type
  2. How to mark an integer as long type
  3. Find out the max/min value and its size for long type
  4. How to create Long object
  5. How to get the sign of the long value
  6. How to get the number of zero bits preceding and following
  7. How to reverse and rotate a long value
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing