Advertisement
viraldim

CountOfEqualBitPairs

May 13th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. /*8.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*/
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class CountOfEqualBitPairs {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.println("Enter a positive integer number!");
  11.         int num =input.nextInt();
  12.         int counter = 0;
  13.         int duoCounts = 0;
  14.         String binNum = Integer.toBinaryString(num);
  15.         char[] binNumChar =binNum.toCharArray();
  16.        
  17.         for (int i = 0; i < binNumChar.length-1; i++) {
  18.             if (binNumChar[i]==binNumChar[i+1]) {
  19.                 counter++;
  20.             }
  21.            
  22.             if (counter ==1) {
  23.                 duoCounts++;
  24.                 counter = 0;
  25.             }
  26.         }
  27.         System.out.println(duoCounts);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement