Writing a binary number system in C code is a fundamental ability for developers, but it is one that must be mastered through practice and study. Practicing basic coding conventions, such as making use of data types, declaring variables, and utilizing both bitwise and logical operations will allow any programmer to gain proficiency in writing binary number systems in C code.
Follow this article to learn how to write binary number systems in C code.
Methods To Write Binary Numbers in C
There are four methods to write binary numbers in C.
Method 1: Iterative Method
One way to write a binary number system in C language using an iterative method is by iterating through a loop and bitwise ANDing any integer with “2^i,” and determining if the ‘i’th bit is 0 (OFF) or 1 (ON).
For a good understanding, see this code.
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(3);
printf("\n");
bin(2);
}
In this code, we took an unsigned integer (32 bit), which has bits 0 through 31. Start at bit 31 and check if it is ON or OFF. If it is ON, print “1”; if it is OFF, print “0.” This will display the binary representation of the unsigned integer. Next, determine if bit 30 is ON or OFF; if ON, print “1”; if OFF, print “0.”The binary representation of the number may be obtained by repeating this procedure for bits 31 through 0.
Output
Method 2: Recursive Method
To print the binary form of a number using a recursive method, your first step will be to check if the number > 1. If it is, push your number on the stack and divide it by 2 recursively till it is still greater than 1. Next, pop that number from the stack and take its ‘mod’ from 2 and print out the remainder.
To get an idea of how the recursive method functions, check this example.
using namespace std;
void bin(unsigned n)
{
if (n > 1)
bin(n / 2);
cout << n % 2;
}
int main(void)
{
bin(1);
cout << endl;
bin(2);
}
In this code, inside the bin() function, first we will determine if the number is greater than 1. If so, we place the number on the stack and recursively divide it by 2 until it is still more than 1 before continuing. Then, we display the residual after taking that number’s “mod” from 2 and popping it off the stack. So the main function will call the bin() function for numbers 1 and 2 to print out their binary form.
Output
Method 3: Recursive Method using Bitwise Operator
To follow this method, check if your number is greater than 0. If it is, right shift the number by 1 bit and call function recursively and then print the bits as the output.
Look at this example.
using namespace std;
void bin(unsigned n)
{
if (n > 1)
bin(n >> 1);
printf("%d", n & 1);
}
int main(void)
{
bin(638);
printf("\n");
bin(498);
return 0;
}
In this code we verify that the number is higher than 0. If so, right-shift the integer by one bit, execute the function repeatedly, and then the bits will be printed.
Output
Method 4: Using Bitset
We can save the binary representation of any integer using the bitset class (positive as well as a negative number). It gives us the freedom to have whichever bits we choose, such as a 32-bit binary representation of a number or only an 8-bit representation.
Here is an example for better understanding.
using namespace std;
int main()
{
int n = 2, m = -2;
bitset<8> b(n);
bitset<8> b1(m);
cout << "Binary of 2:" << b << endl;
cout << "Binary of -2:" << b1 << endl;
return 0;
}
In this code we store 2 and -2 in m and n respectively. They will be converted to the binary form and stored in b and b1, which will then be printed.
Output
Conclusion
When writing binary number system in C code, it is important to understand the basic principles of C programing language. Primarily, this includes understanding the data types used in C (bytes, words, and integers) and how to make use of the data types for writing binary numbers. Additionally, one must have a grasp of coding conventions and syntactical language for putting C commands into action. In the above article, 4 methods are provided to you to write binary number system in C.