Advertisement
nadiahristova

Prob8_CountOfEqualBitPairs

Jan 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prob8_CountOfEqualBitPairs {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.        
  7.         System.out.print("Num = ");
  8.         int num = input.nextInt();
  9.         String str = Integer.toBinaryString(num);
  10.         byte counter = 0;
  11.        
  12.         for (int i = 0; i < (str.length()-1); i++) {
  13.             byte rightBit =(byte)((num >> i) & 1);
  14.             byte leftBit =(byte)((num >> (i+1)) & 1);          
  15.             if (rightBit == leftBit) {
  16.                 counter++;
  17.             }
  18.         }
  19.        
  20.         System.out.println("There are total << " + counter + " >> sequences of two equal bits (\"00\" or \"11\") that can be found in the");
  21.         System.out.printf("binary representation of the num %d.\n",num);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement