Advertisement
M_Andreev

CountOfEqualBitPairs

Sep 7th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _08_CountOfEqualBitPairs {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.        
  8.         System.out.print("Enter number: ");
  9.         int num = sc.nextInt();
  10.         int count = 0;
  11.         int numPreLastDigit;
  12.         int numLastDigit = num & 1;
  13.         num = num >> 1;
  14.        
  15.         while (num > 0) {
  16.             numPreLastDigit = numLastDigit;
  17.             numLastDigit = num & 1;
  18.             num = num >> 1;
  19.            
  20.             if (numPreLastDigit == numLastDigit) {
  21.                 count++;
  22.             }
  23.         }
  24.        
  25.         System.out.println("Result: " + count);
  26.     }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement