Bit Manipulation Operation
From Java Example Source Code
Contents |
[edit] Overview - Bit Manipulation Operation
This Java example program shows how to operate bit.
[edit] Java Source Code
- Package: example.bitset
- File: Bits.java
package example.bitset;
public class Bits {
public static void main(String args[]) {
int n = 170; // 10101010
System.out.println("Value in binary: 10101010");
System.out.println("Number of one bits: " + Integer.bitCount(n));
System.out.println("Highest one bit: " + Integer.highestOneBit(n));
System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));
System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));
System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));
System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
n = 1;
for (int i = 0; i < 16; i++) {
n = Integer.rotateLeft(n, 1);
System.out.println(n);
}}}
[edit] What Result You Can Get
Run the program, you will get:
Value in binary: 10101010 Number of one bits: 4 Highest one bit: 128 Lowest one bit: 2 Number of leading zeros : 24 Number of trailing zeros : 1 Beginning with the value 1, rotate left 16 times. 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
