Binary representation of a given number - Geeks... [Link]
GeeksforGeeks
A computer science portal for geeks
Android App GeeksQuiz
Login/Register
Home
Algorithms
DS
GATE
Interview Corner
Q&A
C
C++
Java
Books
Contribute
Ask a Q
About
Array
Bit Magic
C/C++
Articles
GFacts
Linked List
MCQ
Misc
Output
String
Tree
Graph
Binary representation of a given number
Write a program to print Binary representation of a given number.
Source: Microsoft Interview Set-3
1 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
Method 1: Iterative
For any number, we can check whether its ‘i’th bit is 0(OFF) or 1(ON) by bitwise
ANDing it with “2^i” (2 raise to i).
1) Let us take number 'NUM' and we want to check whether it's 0th bit is ON or OFF
bit = 2 ^ 0 (0th bit)
if NUM & bit == 1 means 0th bit is ON else 0th bit is OFF
2) Similarly if we want to check whether 5th bit is ON or OFF
bit = 2 ^ 5 (5th bit)
if NUM & bit == 1 means its 5th bit is ON else 5th bit is OFF.
Let us take unsigned integer (32 bit), which consist of 0-31 bits. To print binary
representation of unsigned integer, start from 31th bit, check whether 31th bit
is ON or OFF, if it is ON print “1” else print “0”. Now check whether 30th bit is
ON or OFF, if it is ON print “1” else print “0”, do this for all bits from 31 to 0,
finally we will get binary representation of number.
void bin(unsigned n)
{
unsigned i;
for (i = 1 << 31; i > 0; i = i / 2)
(n & i)? printf("1"): printf("0");
}
int main(void)
{
bin(7);
printf("\n");
bin(4);
}
Method 2: Recursive
Following is recursive method to print binary representation of ‘NUM’.
step 1) if NUM > 1
a) push NUM on stack
b) recursively call function with 'NUM / 2'
step 2)
a) pop NUM from stack, divide it by 2 and print it's remainder.
void bin(unsigned n)
{
/* step 1 */
if (n > 1)
bin(n/2);
/* step 2 */
2 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
printf("%d", n % 2);
}
int main(void)
{
bin(7);
printf("\n");
bin(4);
}
This article is compiled by Narendra Kangralkar. Please write comments if
you find anything incorrect, or you want to share more information about the
topic discussed above.
Related Topics:
Check if binary representation of a number is palindrome
Swap two nibbles in a byte
How to turn off a particular bit in a number?
Check if a number is multiple of 9 using bitwise operators
How to swap two numbers without using a temporary variable?
Divide and Conquer | Set 4 (Karatsuba algorithm for fast multiplication)
Find position of the only set bit
Swap all odd and even bits
Like 7 Tweet 2 1
Writing code in comment? Please use [Link] and share the link here.
3 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
35 Comments GeeksforGeeks Login
Sort by Newest Recommend ⤤ Share
Join the discussion…
kapil sharma • 4 days ago
it should be unsigned int n not unsigned n
• Reply • Share ›
Faisal > kapil sharma • 17 hours ago
unsigned alone is same as unsigned int
• Reply • Share ›
9Codie05 • 12 days ago
One simple method considering the size of input as less than 10^9. Please correct me if I
am wrong.
int main()
int n,i,j;
int a[100]={0};
scanf("%d",&n);
i =0;
while(n>0)
a[i]=n%2;
see more
• Reply • Share ›
Girish rao • 13 days ago
#include<stdio.h>
int bin(int x) {
4 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
GeeksforGeeks
Like
91,957 people like GeeksforGeeks.
Facebook social plugin
Interview Experiences
Advanced Data Structures
Dynamic Programming
Greedy Algorithms
Backtracking
Pattern Searching
Divide & Conquer
Mathematical Algorithms
Recursion
Geometric Algorithms
5 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
Popular Posts
All permutations of a given string
Memory Layout of C Programs
Understanding “extern” keyword in C
Median of two sorted arrays
Tree traversal without recursion and without stack!
Structure Member Alignment, Padding and Data Packing
Intersection point of two Linked Lists
Lowest Common Ancestor in a BST.
Check if a binary tree is BST or not
Sorted Linked List to Balanced BST
6 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
Follow @GeeksforGeeks Subscribe
Recent Comments
Ashish Jaiswal
No need to use static variable.....not...
Given a binary tree, print out all of its root-to-leaf paths one per
line. · 31 minutes ago
anuj
For which post you applied ?
Practo Interview Experience | Set 2 (Off-Campus) · 2 hours ago
Sahil Mutneja
Explanation with the Sample Spoj Problem and...
Searching for Patterns | Set 2 (KMP Algorithm) · 3 hours ago
chetan
andidate = 0 count = 0 for value in input: if...
Majority Element · 3 hours ago
chetan
method 3 i don't think its working if we take 2...
Majority Element · 3 hours ago
Guest
Isn't this runtime n^2, because we are...
Search in a row wise and column wise sorted matrix · 3 hours ago
► C++ Programs ► Java String Int ► Find Number
► Binary Number ► The Binary ► Reverse Number
► Java String Int ► C++ Programming ► C++ Code
7 of 8 Thursday 05 March 2015 01:26 PM
Binary representation of a given number - Geeks... [Link]
@geeksforgeeks, Some rights reserved Contact Us!
Powered by WordPress & MooTools, customized by geeksforgeeks team
8 of 8 Thursday 05 March 2015 01:26 PM