Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Practical C Programming

You're reading from   Practical C Programming Solutions for modern C developers to create efficient and well-structured programs

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781838641108
Length 616 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
B. M. Harwani B. M. Harwani
Author Profile Icon B. M. Harwani
B. M. Harwani
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Working with Arrays 2. Managing Strings FREE CHAPTER 3. Exploring Functions 4. Preprocessing and Compilation 5. Deep Dive into Pointers 6. File Handling 7. Implementing Concurrency 8. Networking and Inter-Process Communication 9. Sorting and Searching 10. Working with Graphs 11. Advanced Data Structures and Algorithms 12. Creativity with Graphics 13. Using MySQL Database 14. General-Purpose Utilities 15. Improving the Performance of Your Code 16. Low-Level Programming 17. Embedded Software and IoT 18. Applying Security in Coding 19. Other Books You May Enjoy

Converting a decimal into binary using a bitwise operator

In this recipe, we will learn to convert a decimal number into a binary number by making use of a bitwise operator. Bitwise operators operate on the binary digits of a number and enable us to do precise manipulation as desired.

How to do it…

To convert a decimal number into a binary number by making use of a bitwise operator, perform the following steps:

  1. Enter a decimal number. This number is internally stored in the form of binary digits.
  2. Isolate the least significant bit of the decimal number by applying a logical AND operation between the entered decimal number and value 1.
  3. The least significant bit that results from step 2 is stored in an array.
  4. Right-shift the binary digits of the decimal number by 1 bit. On shifting to the right, the second least significant bit will become the least significant bit.
  5. Repeat steps 2 to 4 until all the binary digits of the decimal number are placed into the array.
  6. The binary digits assigned to the array are the binary version of the entered decimal number. Display the binary digits in an array to get the result.

The program for converting a decimal into a binary number using a bitwise operator is as follows:

convertintobin.c
#include <stdio.h>
void main()
{
int num,i,x,temp;
int p[10];
printf("Enter Decimal Number : ");
scanf("%d",&num);
temp=num;
x=0;
while(num > 0)
{
if((num & 1) == 0 )
{
p[x]=0; x++;
}
else
{
p[x]=1;
x++;
}
num = num >> 1;
}
printf("Binary of %d is ",temp);
for(i=x-1;i>=0;i--)printf("%d",p[i]);
}

Now, let's go behind the scenes.

How it works...

You will be prompted to enter a decimal number. The number you enter is assigned to the num variable. The value entered in the num variable is temporarily assigned to another variable, temp. A while loop is set to execute until the value of num becomes 0. Apply the logical AND operation to isolate each binary digit of the number. For example, if the value entered in variable num is 13, then, internally, it will be stored in a binary format as follows:

Figure 16.2

Now, the least significant bit is isolated by applying the AND operation. That is, the binary digits of 13 are ANDed with 1 as follows. The ANDed means the AND operation is applied on binary digits of 13 and 1 :

Figure 16.3

The application of AND on binary digits 1 and 1 results in 1. If either of the binary digits is 0, then the result of AND will be 0. So, the output of num AND 1 will be 1, which will be stored into array p at index location 0 as shown in the following screenshot:

Figure 16.4

After that, right-shift the digits in the num variable by 1 bit. On shifting to the right, the least significant bit, 1, will be dropped off and a 0 will be added to the most significant bit. Again, the new set of binary digits in the num variables is ANDed with 1, that is, the AND operation is applied between the new set of binary digits in num  variable and 1. The output of num AND 1 will be 0, which is then assigned to array p at index location 1; that is, 0 will be assigned to the p[1] location as shown in the figure:

Figure 16.5

Again, right-shift the digits in the num variable by 1 bit. Again, the least significant bit, 0will be dropped off and a 0 will be added to the most significant bit. Once more, the new set of binary digits in the num variables is ANDed with 1as shown in Figure 16.6(a). The output of the num variable AND 1 will be 1which is then assigned to array p at index location 2. Thereafter, right-shift the digits in the num variable again by 1 bit. The most significant bit in the num variable, 1will be dropped off and a 0 bit will be added to the most significant bit location. The binary digits in num are once more ANDed with 1 . ANDed here means, the AND operation is applied between the binary digits in num and 1. The output of the AND operation will be 1which will be assigned to array p at index location p[3] (Figure 16.6(b)):

Figure 16.6 (a) and (b)

Now, the binary digits assigned to array p are the binary conversion of the number assigned to variable num. Simply display the binary digits in array p in reverse order to get the result. Hence, 1 1 0 1 is the binary conversion of 13.

Let's use GCC to compile the convertintobin.c program as follows:

D:\CBook>gcc convertintobin.c -o convertintobin

If you get no errors or warnings, that means the convertintobin.c program has compiled into an executable file, convertintobin.exe. Let's run this executable file as follows:

D:\CBook>convertintobin
Enter Decimal Number : 13
Binary of 13 is 1101

Voilà! We've successfully converted a decimal number into a binary number using a bitwise operator. Now, let's move on to the next recipe!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image