Advertisement
dimipan80

Count of Equal Bit Pairs

Aug 17th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. /* Write a program to count how many sequences of two equal bits ("00" or "11")
  2.  * can be found in the binary representation of given integer number n (with overlapping). */
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class _08_CountOfEqualBitPairs {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner scan = new Scanner(System.in);
  11.         System.out.print("Enter a Integer number: ");
  12.         int number = scan.nextInt();
  13.  
  14.         int equalBitsCounter = 0;
  15.         while (number != 0) {
  16.             int twoBitsValue = number & 3;
  17.             if (twoBitsValue == 0 || twoBitsValue == 3) {
  18.                 equalBitsCounter++;
  19.             }
  20.  
  21.             number >>>= 1;
  22.         }
  23.  
  24.         System.out.println("The Count of Equal bit pairs in that number is: " + equalBitsCounter);
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement