0% found this document useful (0 votes)
3 views18 pages

Bitwise Operator

Uploaded by

rinapramanick9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Bitwise Operator

Uploaded by

rinapramanick9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/**

* C program to check Least Significant Bit (LSB) of a number using bitwise


operator
*/

#include <stdio.h>
int main()
{
int num;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);

/* If (num & 1) evaluates to 1 */


if(num & 1)
printf("LSB of %d is set (1).", num);
else
printf("LSB of %d is unset (0).", num);

return 0;
}

/**
* C program to check Most Significant Bit (MSB) of a number using bitwise
operator
*/

#include <stdio.h>
#define BITS sizeof(int) * 8 // Total bits required to represent integer

int main()
{
int num, msb;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Move first bit of 1 to highest order */


msb = 1 << (BITS - 1);

/* Perform bitwise AND with msb and num */


if(num & msb)
printf("MSB of %d is set (1).", num);
else
printf("MSB of %d is unset (0).", num);

return 0;
}

/**
* C program to get the nth bit of a number
*/

#include <stdio.h>

int main()
{
int num, n, bitStatus;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Input bit position you want to check */


printf("Enter nth bit to check (0-31): ");
scanf("%d", &n);

/* Right shift num, n times and perform bitwise AND with 1 */


bitStatus = (num >> n) & 1;

printf("The %d bit is set to %d", n, bitStatus);

return 0;
}

/**
* C program to set the nth bit of a number
*/

#include <stdio.h>

int main()
{
int num, n, newNum;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Input bit position you want to set */


printf("Enter nth bit to set (0-31): ");
scanf("%d", &n);

/* Left shift 1, n times and perform bitwise OR with num */


newNum = (1 << n) | num;

printf("Bit set successfully.\n\n");


printf("Number before setting %d bit: %d (in decimal)\n", n, num);
printf("Number after setting %d bit: %d (in decimal)\n", n, newNum);

return 0;
}

/**
* C program to clear the nth bit of a number
*/

#include <stdio.h>

int main()
{
int num, n, newNum;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Input bit number you want to clear */


printf("Enter nth bit to clear (0-31): ");
scanf("%d", &n);
/*
* Left shifts 1 to n times
* Perform complement of above
* finally perform bitwise AND with num and result of above
*/
newNum = num & (~(1 << n));

printf("Bit cleared successfully.\n\n");


printf("Number before clearing %d bit: %d (in decimal)\n", n, num);
printf("Number after clearing %d bit: %d (in decimal)\n", n, newNum);

return 0;
}

/**
* C program to toggle nth bit of a number
*/

#include <stdio.h>

int main()
{
int num, n, newNum;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Input bit position you want to toggle */


printf("Enter nth bit to toggle (0-31): ");
scanf("%d", &n);

/*
* Left shifts 1, n times
* then perform bitwise XOR with num
*/
newNum = num ^ (1 << n);

printf("Bit toggled successfully.\n\n");


printf("Number before toggling %d bit: %d (in decimal)\n", n, num);
printf("Number after toggling %d bit: %d (in decimal)\n", n, newNum);

return 0;
}

/**
* C program to find highest order set bit in a number
*/

#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 /* Integer size in bits */

int main()
{
int num, order = -1, i;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);
/* Iterate over each bit of integer */
for(i=0; i<INT_SIZE; i++)
{
/* If current bit is set */
if((num>>i) & 1)
order = i;
}

if (order != -1)
printf("Highest order set bit in %d is %d", num, order);
else
printf("0 has no set bits.");

return 0;
}

/**
* C program to get lowest order set bit in a number
*/

#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 /* Integer size in bits */

int main()
{
int num, order, i;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/* Initially set the order to max size of integer */


order = INT_SIZE - 1;

/* Iterate through each bit of integer */


for(i=0; i<INT_SIZE; i++)
{
/* If current bit is set */
if((num>>i) & 1)
{
order = i;

/* Terminate the loop */


break;
}
}

printf("Lowest order set bit in %d is %d", num, order);

return 0;
}

/**
* C program to count trailing zeros in a binary number using bitwise operator
*/

#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 /* Bits required to represent an integer */
int main()
{
int num, count, i;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

count = 0;

/* Iterate over each bit of the number */


for(i=0; i<INT_SIZE; i++)
{
/* If set bit is found the terminate from loop*/
if((num >> i ) & 1)
{
/* Terminate from loop */
break;
}

/* Increment trailing zeros count */


count++;
}

printf("Total number of trailing zeros in %d is %d.", num, count);

return 0;
}

/**
* C program to count leading zeros in a binary number using bitwise operator
*/

#include <stdio.h>
#define INT_SIZE sizeof(int) * 8

int main()
{
int num, count, msb, i;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

// Equivalent to
// 10000000 00000000 00000000 00000000
msb = 1 << (INT_SIZE - 1);

count = 0;

/* Iterate over each bit */


for(i=0; i<INT_SIZE; i++)
{
/* If leading set bit is found */
if((num << i) & msb)
{
/* Terminate the loop */
break;
}
count++;
}

printf("Total number of leading zeros in %d is %d", num, count);

return 0;
}

/**
* C program to count flip all bits of a binary number using bitwise operator
*/

#include <stdio.h>

int main()
{
int num, flippedNumber;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

flippedNumber = ~num;

printf("Original number = %d (in decimal)\n", num);


printf("Number after bits are flipped = %d (in decimal)", flippedNumber);

return 0;
}
/**
* C program to rotate bits of a number.
*/

#include <stdio.h>

#define INT_SIZE sizeof(int) // Size of int in bytes


#define INT_BITS INT_SIZE * 8 - 1 // Size of int in bits - 1

/* Function declarations */
int rotateLeft(int num, unsigned int rotation);
int rotateRight(int num, unsigned int rotation);

int main()
{
int num;
unsigned int rotation;

/* Input number from user */


printf("Enter a number: ");
scanf("%d", &num);

/* Input number of rotation */


printf("Enter number of rotation: ");
scanf("%u", &rotation);
/* Print rotated number */
printf("%d left rotated %u times = %d\n\n", num, rotation, rotateLeft(num,
rotation));
printf("%d right rotated %u times = %d\n", num, rotation, rotateRight(num,
rotation));

return 0;
}

/**
* Function to rotate bits of a number to left.
*
* @num Number to rotate.
* @rotation Number of times to rotate left.
*/
int rotateLeft(int num, unsigned int rotation)
{
int DROPPED_MSB;

// The effective rotation


rotation %= INT_BITS;

// Loop till rotation becomes 0


while(rotation--)
{
// Get MSB of num before it gets dropped
DROPPED_MSB = (num >> INT_BITS) & 1;
// Left rotate num by 1 and
// Set its dropped MSB as new LSB
num = (num << 1) | DROPPED_MSB;
}

return num;
}

/**
* Function to rotate bits of a number to right.
*
* @num Number to rotate.
* @rotation Number of times to rotate right.
*/
int rotateRight(int num, unsigned int rotation)
{
int DROPPED_LSB;

// The effective rotation


rotation %= INT_BITS;

// Loop till rotation becomes 0


while(rotation--)
{
// Get LSB of num before it gets dropped
DROPPED_LSB = num & 1;
// Right shift num by 1 and
// Clear its MSB
num = (num >> 1) & (~(1 << INT_BITS));

// Set its dropped LSB as new MSB


num = num | (DROPPED_LSB << INT_BITS);
}

return num;
}

/**
* C program to convert decimal to binary number system
*/

#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 /* Size of int in bits */

int main()
{
int num, index, i;
int bin[INT_SIZE];

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

index = INT_SIZE - 1;
while(index >= 0)
{
/* Store LSB of num to bin */
bin[index] = num & 1;

/* Decrement index */
index--;

/* Right Shift num by 1 */


num >>= 1;
}

/* Print converted binary */


printf("Converted binary: ");
for(i=0; i<INT_SIZE; i++)
{
printf("%d", bin[i]);
}

return 0;
}

/**
* C program to swap two numbers using bitwise operator
*/

#include <stdio.h>

int main()
{
int num1, num2;

/* Input two numbers from user */


printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

printf("Original value of num1 = %d\n", num1);


printf("Original value of num2 = %d\n", num2);

/* Swap two numbers */


num1 ^= num2;
num2 ^= num1;
num1 ^= num2;

printf("Num1 after swapping = %d\n", num1);


printf("Num2 after swapping = %d\n", num2);

return 0;
}

/**
* C program to check even or odd number using bitwise operator
*/

#include <stdio.h>

int main()
{
int num;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);

if(num & 1)
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}

return 0;
}

You might also like