Python Dictionary | Check if binary representations of two numbers are anagram Last Updated : 07 Nov, 2025 Comments Improve Suggest changes 6 Likes Like Report Given two numbers, the task is to check whether their binary representations are anagrams of each other or not. Two binary numbers are said to be anagrams if they contain the same number of 0s and 1s, irrespective of their order.Example:Input: a = 8, b = 4 Output: YesExplanation: Binary representation of 8: 1000Binary representation of 4: 0100Both contain three 0s and one 1, hence they are anagrams.Using Bitwise OperationsThis is the most efficient method as it works directly on bit counts without converting to large strings. Python a, b = 8, 4 b1 = bin(a).count('1') b2 = bin(b).count('1') x = max(a.bit_length(), b.bit_length()) c1 = x - b1 c2 = x - b2 if b1 == b2 and c1 == c2: print("Yes") else: print("No") OutputYes Explanation:bin(a).count('1'): counts number of 1s in binary form.bit_length(): gives the number of bits required to represent the number.0s are found indirectly as total bits minus 1s.If both have equal counts of 0s and 1s, their binaries are anagrams.Using zfill() and Counting BitsFirst convert both numbers into 32-bit binary strings using zfill(), then count the number of 0s and 1s in each. Python a, b = 8, 4 b1 = bin(a)[2:].zfill(32) b2 = bin(b)[2:].zfill(32) c1 = [b1.count('0'), b1.count('1')] c2 = [b2.count('0'), b2.count('1')] if c1 == c2: print("Yes") else: print("No") OutputYes Explanation:bin() converts the given integer into binary strings.[2:] removes the '0b' prefix from the binary strings..zfill(32) pads zeros on the left, ensuring equal length (32 bits).[b1.count('0') and b1.count('1')]: count 0s and 1s and create a list.If both the lists matches then binaries are anagrams.Using collections.Counter and Dictionary ComparisonThis approach uses the Counter class from Python’s collections module to count the frequency of 0s and 1s, and then compares the two resulting dictionaries. Python from collections import Counter a, b = 8, 4 b1 = bin(a)[2:] b2 = bin(b)[2:] pad = abs(len(b1) - len(b2)) if len(b1) > len(b2): b2 = '0' * pad + b2 else: b1 = '0' * pad + b1 c1 = Counter(b1) c2 = Counter(b2) if c1 == c2: print("Yes") else: print("No") OutputYes Explanation:bin(a)[2:], bin(b)[2:]: binary strings without '0b'.'0' * pad + b2: adds zeros to equalize lengths.Counter(): creates frequency maps of 0s and 1s.Equal counters: binaries are anagrams.Related Articles:Python Bitwise OperatorsPython Operators Create Quiz Comment S Shashank Mishra Follow 6 Improve S Shashank Mishra Follow 6 Improve Article Tags : Python base-conversion python-dict Python dictionary-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like