Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class CountEqualBitPairs {
- public static void main(String[] args) {
- System.out.println("Enter the number which bits will be compared");
- Scanner inputScanner = new Scanner(System.in);
- int number = inputScanner.nextInt();
- int semiNumber = number >> 1;
- int count = 0;
- while (semiNumber != 0) {
- int numberBit = 1 & number;
- int semiNumberBit = 1 & semiNumber;
- if (numberBit == semiNumberBit) {
- count ++;
- }
- semiNumber = semiNumber >> 1;
- number = number >> 1;
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement