# Check if binary representation of a given number and its complement are anagram

Given a positive number you need to check whether it’s complement and the number are anagrams or not.

**Examples:**

Input : a = 4294967295 Output : Yes Binary representation of 'a' and it's complement are anagrams of each other Input : a = 4 Output : No

**Simple Approach: ** In this approach calculation of the complement of the number is allowed.

1. Find binary representation of the number and it’s complement using simple decimal to binary representation technique.

2. Sort both the binary representations and compare them to check whether they are anagrams or not.

`// A simple C++ program to check if binary ` `// representations of a number and it's ` `// complement are anagram. ` `#include <bits/stdc++.h> ` `#define ull unsigned long long int ` `using` `namespace` `std; ` ` ` `const` `int` `ULL_SIZE = 8*` `sizeof` `(ull); ` ` ` `bool` `isComplementAnagram(ull a) ` `{ ` ` ` `ull b = ~a; ` `// Finding complement of a; ` ` ` ` ` `// Find reverse binary representation of a. ` ` ` `bool` `binary_a[ULL_SIZE] = { 0 }; ` ` ` `for` `(` `int` `i=0; a > 0; i++) ` ` ` `{ ` ` ` `binary_a[i] = a % 2; ` ` ` `a /= 2; ` ` ` `} ` ` ` ` ` `// Find reverse binary representation ` ` ` `// of complement. ` ` ` `bool` `binary_b[ULL_SIZE] = { 0 }; ` ` ` `for` `(` `int` `i=0; b > 0; i++) ` ` ` `{ ` ` ` `binary_b[i] = b % 2; ` ` ` `b /= 2; ` ` ` `} ` ` ` ` ` `// Sort binary representations and compare ` ` ` `// after sorting. ` ` ` `sort(binary_a, binary_a + ULL_SIZE); ` ` ` `sort(binary_b, binary_b + ULL_SIZE); ` ` ` `for` `(` `int` `i = 0; i < ULL_SIZE; i++) ` ` ` `if` `(binary_a[i] != binary_b[i]) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ull a = 4294967295; ` ` ` `cout << isComplementAnagram(a) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

1

**Efficient Approach:** Just count the number of 1’s present in the bit representation of the given number. If number of 1’s present are 32 then it’s complement will also have 32 1’s in it’s bit representation and they will be anagrams of each other.

## C++

`// An efficient C++ program to check if binary ` `// representations of a number and it's complement are anagram. ` `#include <bits/stdc++.h> ` `#define ull unsigned long long int ` `using` `namespace` `std; ` ` ` `const` `int` `ULL_SIZE = 8*` `sizeof` `(ull); ` ` ` `// Returns true if binary representations of ` `// a and b are anagram. ` `bool` `bit_anagram_check(ull a) ` `{ ` ` ` `// _popcnt64(a) gives number of 1's present ` ` ` `// in binary representation of a. If number ` ` ` `// of 1s is half of total bits, return true. ` ` ` `return` `(_popcnt64(a) == (ULL_SIZE >> 1)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `ull a = 4294967295; ` ` ` `cout << bit_anagram_check(a) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// An efficient Java program to check if binary ` `// representations of a number and it's complement are anagram. ` `class` `GFG ` `{ ` ` ` `static` `byte` `longSize = ` `8` `; ` `static` `int` `ULL_SIZE = ` `8` `*longSize; ` ` ` `// Returns true if binary representations of ` `// a and b are anagram. ` `static` `boolean` `bit_anagram_check(` `long` `a) ` `{ ` ` ` `// _popcnt64(a) gives number of 1's present ` ` ` `// in binary representation of a. If number ` ` ` `// of 1s is half of total bits, return true. ` ` ` `return` `(Integer.bitCount((` `int` `)a) == (ULL_SIZE >> ` `1` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `long` `a = 4294967295L; ` ` ` `System.out.println(bit_anagram_check(a)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`# An efficient Python3 program to check ` `# if binary representations of a number ` `# and it's complement are anagram. ` `ULL_SIZE ` `=` `64` ` ` `# Returns true if binary representations of ` `# a and b are anagram. ` `def` `bit_anagram_check(a): ` ` ` ` ` `#_popcnt64(a) gives number of 1's present ` ` ` `# in binary representation of a. If number ` ` ` `# of 1s is half of total bits, return true. ` ` ` `return` `(` `bin` `(a).count(` `"1"` `) ` `=` `=` `(ULL_SIZE >> ` `1` `)) ` ` ` `# Driver Code ` `a ` `=` `4294967295` `print` `(` `int` `(bit_anagram_check(a))) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// An efficient C# program to check ` `// if binary representations of ` `// a number and it's complement ` `// are anagram. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `byte` `longSize = 8; ` `static` `int` `ULL_SIZE = 8*longSize; ` ` ` `// Returns true if binary representations of ` `// a and b are anagram. ` `static` `bool` `bit_anagram_check(` `long` `a) ` `{ ` ` ` `// _popcnt64(a) gives number of 1's present ` ` ` `// in binary representation of a. If number ` ` ` `// of 1s is half of total bits, return true. ` ` ` `return` `(BitCount((` `int` `)a) == (ULL_SIZE >> 1)); ` `} ` ` ` `static` `int` `BitCount(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` `count++; ` ` ` `n &= (n - 1); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `long` `a = 4294967295L; ` ` ` `Console.WriteLine(bit_anagram_check(a)); ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// An efficient PHP program to check ` `// if binary representations of ` `// a number and it's complement ` `// are anagram. ` ` ` `// Returns true if binary representations ` `// of a and b are anagram. ` `function` `bit_anagram_check(` `$a` `) ` `{ ` ` ` `$longSize` `= 8; ` ` ` `$ULL_SIZE` `= 8 * ` `$longSize` `; ` ` ` ` ` `// _popcnt64(a) gives number of 1's present ` ` ` `// in binary representation of a. If number ` ` ` `// of 1s is half of total bits, return true. ` ` ` `return` `(BitCount(` `$a` `) == (` `$ULL_SIZE` `>> 1)); ` `} ` ` ` `function` `BitCount(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `!= 0) ` ` ` `{ ` ` ` `$count` `++; ` ` ` `$n` `&= (` `$n` `- 1); ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$a` `= 4294967295; ` `echo` `(bit_anagram_check(` `$a` `)); ` ` ` `// This code contributed by Rajput-Ji ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

**Note: **

1. The answer is only dependent on the number, in the above approach we don’t even find the need to obtain the complement of the number.

2. The above code uses GCC specific functions. If we wish to write code for other compilers, we may use Count set bits in an integer.

This article is contributed by **Aditya Gupta**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check if binary representation of a number is palindrome
- Check if actual binary representation of a number is palindrome
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check if binary representations of two numbers are anagram
- 1's and 2's complement of a Binary Number
- Check if all the set bits of the binary representation of N are at least K places away
- Binary representation of a given number
- Binary representation of next number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Number of leading zeros in binary representation of a given number
- Prime Number of Set Bits in Binary Representation | Set 2
- Prime Number of Set Bits in Binary Representation | Set 1
- Find the n-th number whose binary representation is a palindrome
- Occurrences of a pattern in binary representation of a number
- C program to count zeros and ones in binary representation of a number
- Number of mismatching bits in the binary representation of two integers
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number