Advertisement
Guest User

08.CountOfEqualBitPairs

a guest
May 12th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class CountOfEqualBitPairs {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int number = input.nextInt();
  9.         int mask = 3;
  10.         int count = 0;
  11.         int leftmost =0;
  12.         if(number!=0){
  13.             for(int i = 0;i<32;i++){
  14.                 if((number&1<<i)>0)
  15.                 {
  16.                     leftmost = i;
  17.                 }
  18.             }
  19.             for(int i = 0;i<=leftmost;i++){
  20.                 if(((mask<<i&number)>>i)==3){
  21.                     count++;
  22.                 }
  23.                 else if(((mask<<i&~number)>>i)==3){
  24.                     count++;
  25.                 }
  26.             }
  27.             System.out.println(count);
  28.         }
  29.         else {
  30.             System.out.println(0);
  31.         }
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement