Check The Programming Section
The smallest unit of memory in which we have worked so far is a byte and smallest data type is char. We have not tried yet, how the basic data types are constructed on individual bits and how these individual bits can be manipulated. Operating on bit level is very important for programming, when a program interacts with hardware directly. This is because programming languages are designed as byte oriented and hardwares are designed to be bit oriented.
C/C++ supports a set of bit manipulation operators and this set of operators permits programmers to access and manipulate individual bits of data. The various bit wise operators are listed below:
Note: These bitwise operators work with int and char data type only but not with float, double, long double, and void data types.
Before starting on the bit manipulations with these operators, let's first look at the bit representation of chars and ints data types. In computer memory, every bits are numbered and starts with zero (0) and moves from right to left.
Before starts this discussion we must know how to convert an integer and character to binary number. Binary numbers are number system of base 2 and computer hardwares only understand binary numbers. These binary numbers are formed with 0's and 1's.
At first, the process is started with a division operation, an integer number is divided with 2 and save the reminder
Divide the quotient part of the previous division by 2 and save the reminder
Repeat step 2 till the quotient become zero (0)
Now, placed all the reminder from bottom to top
The illustration to get the binary number is shown below:
The equivalent binary of integer 25 is 11001.
To get the one’s complement of a number, we simply flip all the 1’s to 0 and all the 0’s to 1. For example, one’s complement of 1001100 is 0110011 and similarly one’s complement of 1111 is 0000.
#include<stdio.h>
//This function calculates the binary form of the number
void getBits(int bin[], short int num){
int i = 15;
while(num>0){
bin[i] = num%2;
i--;
num/=2;
}
}
//It prints the bits of the number
void printBits(int bin[]){
for(int i=0;i<=15;i++){
printf("%d",bin[i]);
}
}
int main(){
short int num = 25, k;
short int bin[16]= {0};
getBits(bin, num);
printf("Binary of %d is ::",num);
printBits(bin);
//applying One's Complement Operator
k = ~num;
//flipping the bits
for(int i=0;i<=15;i++)
bin[i]==0?bin[i]=1:bin[i]=0;
printf("\n");
printf("The 1's complement of %d is %d :: ",num, k);
printBits(bin);
}
Here, I have used two functions to get the binary form of a integer number.
The function getBits() saves the reminders of an integer number by dividing it by 2 and till the quotient become 0.
The function printBits() is used to print the bits
At first, an array bin with size 16 is get populated to 0 and this array is taken to save the reminders, while dividing a interger number by 2. Inside main() the 1's complement operator (~) is used in statement k = ~num; to get the 1's complement of num.