Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Problem08_CountEqualBits {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n;
- int bit;
- int nextBit;
- int count = 0;
- int pos;
- int j = 31;
- System.out.print("n= ");
- n = input.nextInt();
- for ( j = 31; j >= 0; j--) { //finds the position (j) of the first bit 1
- pos = (int)Math.pow(2, j)& n;
- //System.out.println(Integer.toBinaryString(pos));
- if (pos != 0) {
- break;
- }
- }
- for (int i = 0; i < j; i++) {
- bit = n & 1;
- n = n >> 1;
- nextBit = n & 1;
- if (bit == nextBit) {
- count++;
- }
- }
- System.out.printf("Equal bit pairs are: %d", count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement