0% found this document useful (0 votes)
41 views19 pages

1

Uploaded by

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

1

Uploaded by

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

#include <stdio.

h>

// Macros for bitwise operations


#define GET_BITS(num, n) ((num) & ((1 << (n)) - 1))
#define SET_BITS(num1, num2, n) (((num1) & ~((1 << (n)) - 1)) | ((num2) & ((1 <<
(n)) - 1)))
#define GET_BITS_FROM_POSITION(num, pos, n) (((num) >> (pos)) & ((1 << (n)) - 1))
#define SET_BITS_FROM_POSITION(num1, num2, pos, n) (((num1) & ~(((1 << (n)) - 1) <<
(pos))) | (((num2) & ((1 << (n)) - 1)) << (pos)))
#define TOGGLE_BITS_FROM_POSITION(num, pos, n) ((num) ^ (((1 << (n)) - 1) <<
(pos)))
#define PRINT_BITS(num) do { int i; for (i = sizeof(num) * 8 - 1; i >= 0; i--)
{ printf("%d", (num & (1 << i)) ? 1 : 0); } printf("\n"); } while(0)

// Macro to copy bits from start position to end position


#define COPY_BITS(num, start, end) (((num) & (((1 << ((end) - (start) + 1)) - 1) <<
(start))) >> (start))

int main() {
int choice;
unsigned int num1, num2, result;
int n, pos, start, end;

while (1) {
// Display menu
printf("\nMenu:\n");
printf("1. Get n bits from a number\n");
printf("2. Set n bits of a number with the n bits of another number\n");
printf("3. Get n bits from specified position\n");
printf("4. Set n bits of a number with the n bits of another number from
specified position\n");
printf("5. Toggle n bits from specified position\n");
printf("6. Print the bits of the given number\n");
printf("7. Copy bits from start position to end position\n");
printf("0. Exit\n");

// Get user choice


printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a number: ");
scanf("%u", &num1);
printf("Enter the number of bits to get: ");
scanf("%d", &n);
result = GET_BITS(num1, n);
printf("Result: %u\n", result);
break;

case 2:
printf("Enter the first number: ");
scanf("%u", &num1);
printf("Enter the second number: ");
scanf("%u", &num2);
printf("Enter the number of bits to set: ");
scanf("%d", &n);
result = SET_BITS(num1, num2, n);
printf("Result: %u\n", result);
break;

case 3:
printf("Enter a number: ");
scanf("%u", &num1);
printf("Enter the starting position: ");
scanf("%d", &pos);
printf("Enter the number of bits to get: ");
scanf("%d", &n);
result = GET_BITS_FROM_POSITION(num1, pos, n);
printf("Result: %u\n", result);
break;

case 4:
printf("Enter the first number: ");
scanf("%u", &num1);
printf("Enter the second number: ");
scanf("%u", &num2);
printf("Enter the starting position: ");
scanf("%d", &pos);
printf("Enter the number of bits to set: ");
scanf("%d", &n);
result = SET_BITS_FROM_POSITION(num1, num2, pos, n);
printf("Result: %u\n", result);
break;

case 5:
printf("Enter a number: ");
scanf("%u", &num1);
printf("Enter the starting position: ");
scanf("%d", &pos);
printf("Enter the number of bits to toggle: ");
scanf("%d", &n);
result = TOGGLE_BITS_FROM_POSITION(num1, pos, n);
printf("Result: %u\n", result);
break;

case 6:
printf("Enter a number: ");
scanf("%u", &num1);
printf("Binary representation: ");
PRINT_BITS(num1);
break;

case 7:
printf("Enter a number: ");
scanf("%u", &num1);
printf("Enter the starting position: ");
scanf("%d", &start);
printf("Enter the ending position: ");
scanf("%d", &end);
result = COPY_BITS(num1, start, end);
printf("Result: %u\n", result);
break;

case 0:
// Exit the program
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

-------------------------------------------------------
#ifndef BITWISE_OPERATIONS_H
2 #define BITWISE_OPERATIONS_H
3
4 // Macros for bitwise operations
5 #define GET_BITS(num, n) ((num) & ((1 << (n)) - 1))
6 #define SET_BITS(num1, num2, n) (((num1) & ~((1 << (n)) - 1)) | ((num2) & ((1
<< (n)) - 1)))
7 #define GET_BITS_FROM_POSITION(num, pos, n) (((num) >> (pos)) & ((1 << (n)) -
1))
8 #define SET_BITS_FROM_POSITION(num1, num2, pos, n) (((num1) & ~(((1 << (n)) -
1) << (pos))) | (((num2) & ((1 << (n)) - 1)) << (pos)))
9 #define TOGGLE_BITS_FROM_POSITION(num, pos, n) ((num) ^ (((1 << (n)) - 1) <<
(pos)))
10 #define PRINT_BITS(num)
11 do
12 {
13 int i;
14 for (i = sizeof(num) * 8 - 1; i >= 0; i--)
15 {
16 printf("%d", (num & (1 << i)) ? 1 : 0);
17 } printf("\n");
18 } while(0)
19 #define COPY_BITS(num, start, end) (((num) & (((1 << ((end) - (start) + 1)) -
1) << (start))) >> (start))
20
21 #endif // BITWISE_OPERATIONS_H
22

unsigned int readUnsignedInt(const char *prompt) {


unsigned int value;

while (1) {
printf("%s", prompt);
if (scanf("%u", &value) == 1) {
// Valid input, break out of the loop
break;
} else {
// Invalid input, clear buffer
while (getchar() != '\n');
printf("Invalid input. Please enter a valid unsigned integer.\n");
}
}

return value;
}
printf("\033[33m%d\033[0m", ((num) >> (31 - i)) & 0x01); \

#include <stdio.h>
#include "bitwise_function.h"
#include "header.h"

int main() {
int choice;
char choice1;
unsigned int num1, num2, result;
int n, pos, start, end;

while (1) {
// Display menu
printf("\
033[38m............................MENU..................................\033[0m\
n");
printf("1. C programs to implement below bitwise operations:\n");
printf("a. Get n bits from a number\n");
printf("b. Set n bits of a number with the n bits of another number\n");
printf("c. Get n bits from specified position\n");
printf("d. Set n bits of a number with the n bits of another number from
specified position\n");
printf("e. Toggle n bits from specified position\n");
printf("f. Print the bits of the given number\n");
printf("2. Copy bits from start position to end position\n");
printf("0. Exit\n");
printf("\n..........................END OF
MENU....................................\n");

printf("Please Enter your choice: \n");


if (scanf(" %c", &choice1) != 1) {
// Invalid input, clear buffer
while(getchar() != '\n');
printf("Invalid input. Please enter a correct choice.\n");
continue;
}

// Get user choice


if (scanf("%d", &choice) != 1) {
// Invalid input, clear buffer
while (getchar() != '\n');
printf("Invalid input. Please enter a number.\n");
continue;
}

if (choice == 0) {
printf("Exiting program.\n");
break;
}

if ((choice >= 1 && choice <= 6) && (choice1 >= 'a' && choice1 <= 'f')) {
switch (choice) {
case 1:
// Operation a
// ... (your code for operation a)
break;

case 2:
// Operation b
// ... (your code for operation b)
break;

// Add cases for c, d, e, f as needed

default:
printf("Invalid choice. Please try again.\n");
}
} else {
printf("ERROR: Please pass a valid choice.\n");
}
}

return 0;
}

#include <stdio.h>

// Macro to count the number of bits in a number


#define COUNT_BITS(num) (sizeof(num) * 8)

// Macro to print the binary representation of a number


#define PRINT_BITS(num) \
do { \
printf("Binary Representation: "); \
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) { \
printf("%d", (num >> i) & 1); \
} \
printf("\n"); \
} while (0)

int main() {
unsigned int num, result;

num = 0; // Initialize num with your desired value

printf("Please enter the first number: ");


scanf("%u", &num);
PRINT_BITS(num);

// Counting the number of bits in the number using the macro


result = COUNT_BITS(num);

// Comparing the bits for palindrome


int isPalindrome = 1; // Assume it's a palindrome

for (int i = 0; i < result / 2; i++) {


int leftBit = (num >> i) & 1;
int rightBit = (num >> (result - 1 - i)) & 1;

if (leftBit != rightBit) {
isPalindrome = 0; // It's not a palindrome
break; // No need to check further
}
}

if (isPalindrome) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}

#include <stdio.h>

// Function to read an unsigned integer from the user


unsigned int readUnsignedInt(const char *prompt) {
unsigned int value;
printf("%s", prompt);
scanf("%u", &value);
return value;
}

// Macro to count the number of bits in a number


#define COUNT_BITS(num) (sizeof(num) * 8)

// Macro to check if the bit pattern is a palindrome


#define IS_PALINDROME(num) { \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
}

int main() {
unsigned int num, result;

// Read an unsigned integer from the user


num = readUnsignedInt("Please enter the first number: ");

// Print the binary representation of the number


printf("Binary Representation: ");
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}

-------------------------------------------------------------------------------
#include <stdio.h>

#define COUNT_BITS(num) ({ \
int count = 0; \
while (num) { \
count++; \
num >>= 1; \
} \
count; \
})

#define IS_PALINDROME(num) ({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})

int main() {
int num = 0b11001100110011; // Replace this with your number

// Print the binary representation of the number


printf("Binary Representation: ");
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}
------------------------------------------------------
#include <stdio.h>

#define COUNT_BITS(num) ({ \
int count = 0; \
while (num) { \
count++; \
num >>= 1; \
} \
count; \
})

#define IS_PALINDROME(num) ({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})

int main() {
int num;

// Read a number from the user


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

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}

#include <stdio.h>

#define SET_BITS_FROM_POSITION(num, num2, pos, n) \


(((num) & ~(((1 << (n)) - 1) << (pos))) | (((num2) & ((1 << (n)) - 1)) <<
(pos)))

void printBits(int value, int n) {


for (int i = n - 1; i >= 0; i--) {
printf("\033[33m%d\033[0m", (value >> i) & 1);
}
}

void printBinary(int num) {


printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

unsigned int readUnsignedInt(const char *message) {


unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

int main() {
unsigned int num, num2, pos, n, result;

// Get input from the user


num = readUnsignedInt("Please enter the first number: ");
printBinary(num);

num2 = readUnsignedInt("Please enter the second number: ");


printBinary(num2);

pos = readUnsignedInt("Please enter the starting position: ");


n = readUnsignedInt("Enter the number of bits to set: ");

// Set bits from position


result = SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the extracted setting bits


printf("Extracted setting bits: ");
printBits(result, n);
printf("\n");

// Print the binary representation after setting the bits


printf("After setting extracted bit number is: ");
printBinary(result);

printf("\033[33mResult: %d\033[0m\n", result);

return 0;
}

115//set n bits of a number with n bits of another number from specified position
//set n bits of a number with n bits of another number from specified position
#include <stdio.h>

#define SET_BITS_FROM_POSITION(num, num2, pos, n) \


(((num) & ~(((1 << (n)) - 1) << (pos))) | (((num2) & ((1 << (n)) - 1)) <<
(pos)))

void printBits(int value, int n) {


for (int i = n - 1; i >= 0; i--) {
printf("\033[33m%d\033[0m", (value >> i) & 1);
}
}

void printBinary(int num) {


printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

unsigned int readUnsignedInt(const char *message) {


unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

int main() {
unsigned int num, num2, pos, n, result;

// Get input from the user


num = readUnsignedInt("Please enter the first number: ");
printBinary(num);

num2 = readUnsignedInt("Please enter the second number: ");


printBinary(num2);

pos = readUnsignedInt("Please enter the starting position: ");


n = readUnsignedInt("Enter the number of bits to set: ");

// Set bits from position


result = SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the extracted setting bits


printf("Extracted setting bits: ");
printBits(result, n);
printf("\n");

// Print the binary representation after setting the bits


printf("After setting extracted bit number is: ");
printBinary(result);

printf("\033[33mResult: %d\033[0m\n", result);

return 0;
}

#include <stdio.h>

#define SET_BITS_FROM_POSITION(num, num2, pos, n) \


(((num) & ~(((1 << (n)) - 1) << (pos))) | (((num2) & ((1 << (n)) - 1)) <<
(pos)))

void printBits(int value, int n) {


for (int i = n - 1; i >= 0; i--) {
printf("\033[33m%d\033[0m", (value >> i) & 1);
}
}

void printBinary(int num) {


printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

unsigned int readUnsignedInt(const char *message) {


unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

int main() {
unsigned int num, num2, pos, n, result;

// Get input from the user


num = readUnsignedInt("\033[35mPlease enter the first number:\033[0m ");
printf("\033[35mEntered number:\033[0m ");
printBinary(num);

num2 = readUnsignedInt("\033[35mPlease enter the second number:\033[0m ");


printf("\033[35mEntered number:\033[0m ");
printBinary(num2);

pos = readUnsignedInt("\033[35mPlease enter the starting position:\033[0m ");


n = readUnsignedInt("\033[35mEnter the number of bits to set:\033[0m ");

// Set bits from position


result = SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the extracted setting bits from num2


printf("\033[35mExtracted setting bits from num2:\033[0m ");
printBits(num2, n);
printf("\n");

// Print the binary representation after setting the bits


printf("\033[35mAfter setting extracted bit number is:\033[0m ");
printBinary(result);
printf("\n");

return 0;
}
#include <stdio.h>

#define SET_BITS_FROM_POSITION(num, num2, pos, n) \


(((num) & ~(((1 << (n)) - 1) << (pos))) | (((num2) & ((1 << (n)) - 1)) <<
(pos)))

void printBits(int value, int n) {


for (int i = n - 1; i >= 0; i--) {
printf("\033[33m%d\033[0m", (value >> i) & 1);
}
}

void printBinary(int num) {


printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

unsigned int readUnsignedInt(const char *message) {


unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

int main() {
unsigned int num, num2, pos, n, result;

// Get input from the user


num = readUnsignedInt("Please enter the first number: ");
printf("Entered number: ");
printBinary(num);

num2 = readUnsignedInt("Please enter the second number: ");


printf("Entered number: ");
printBinary(num2);

pos = readUnsignedInt("Please enter the starting position: ");


n = readUnsignedInt("Enter the number of bits to set: ");

// Set bits from position


result = SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the extracted setting bits from num2


printf("Extracted setting bits from num2: ");
printBits(num2, n);
printf("\n");

// Print the binary representation after setting the bits


printf("After setting extracted bit number is: ");
printBinary(result);
printf("\n");

return 0;
}
#include <stdio.h>
#define SET_BITS_FROM_POSITION(num, num2, pos, n) \
(((num) & ~(((1 << (n)) - 1) << (pos))) | (((num2) & ((1 << (n)) - 1)) <<
(pos)))

void printBits(int value, int n) {


for (int i = n - 1; i >= 0; i--) {
printf("\033[33m%d\033[0m", (value >> i) & 1);
}
}

void printBinary(int num) {


printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

unsigned int readUnsignedInt(const char *message) {


unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

int main() {
unsigned int num, num2, pos, n, result;

// Get input from the user


num = readUnsignedInt("Please enter the first number: ");
printf("Entered number: ");
printBinary(num);

num2 = readUnsignedInt("Please enter the second number: ");


printf("Entered number: ");
printBinary(num2);

pos = readUnsignedInt("Please enter the starting position: ");


n = readUnsignedInt("Enter the number of bits to set: ");

// Set bits from position


result = SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the extracted setting bits from num2


printf("Extracted setting bits from num2: ");
printBits(num2, n);
printf("\n");

// Print the binary representation after setting the bits


printf("After setting extracted bit number is: ");
printBinary(result);
printf("\n");

return 0;
}
#include <stdio.h>

#define COUNT_BITS(num) ((int)(sizeof(num) * 8))


#define IS_PALINDROME(num) \
({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})

int main() {
unsigned int num;

// Input the number from the user


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

// Print the binary representation of the number


printf("Binary Representation: ");
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}

#include <stdio.h>

#define COUNT_BITS(num) ((int)(sizeof(num) * 8))

#define IS_PALINDROME(num) \
({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})

int main() {
unsigned int num;

// Input the number from the user


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

// Print the binary representation of the number


printf("Binary Representation: ");
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}
#include <stdio.h>

// Macro to count the number of bits in a given data type


#define COUNT_BITS(num) ((int)(sizeof(num) * 8))

// Macro to check if the bit pattern of a number is a palindrome


#define IS_PALINDROME(num) \
({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})

// Function to read an unsigned integer from the user


unsigned int readUnsignedInt(const char *message) {
unsigned int value;
printf("%s", message);
scanf("%u", &value);
return value;
}

// Function to print the binary representation of a number


void printBinary(unsigned int num) {
printf("Binary Representation: ");
for (int i = COUNT_BITS(num) - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}
int main() {
unsigned int num;

// Input the number from the user


num = readUnsignedInt("Please Enter a number: ");
printBinary(num);

// Check if the bit pattern is a palindrome


if (IS_PALINDROME(num)) {
printf("Bit pattern of the number is a palindrome.\n");
} else {
printf("Bit pattern of the number is not a palindrome.\n");
}

return 0;
}

// Macro to count the number of bits in a given data type


#define COUNT_BITS(num) ((int)(sizeof(num) * 8))

// Macro to check if the bit pattern of a number is a palindrome for 8 bits


#define IS_PALINDROME(num) \
({ \
int isPalindrome = 1; \
for (int i = 0; i < COUNT_BITS(num) / 2; i++) { \
int leftBit = (num >> i) & 1; \
int rightBit = (num >> (COUNT_BITS(num) - 1 - i)) & 1; \
if (leftBit != rightBit) { \
isPalindrome = 0; \
break; \
} \
} \
isPalindrome; \
})
-------------------------------------------------------------------------
#include <stdio.h>

// Macro for pre-increment using bitwise operators


#define PRE_INCREMENT(ptr) (++(*ptr))

// Macro for post-increment using bitwise operators


#define POST_INCREMENT(ptr) ((*ptr)++)

int main() {
int num = 5;

printf("Initial value: %d\n", num);

// Implementing pre-increment using the macro


PRE_INCREMENT(&num);
printf("After pre-increment: %d\n", num);

// Implementing post-increment using the macro


POST_INCREMENT(&num);
printf("After post-increment: %d\n", num);

return 0;
}
----------------------------------------------------------
#include <stdio.h>

// Macro for pre-increment using bitwise operators


#define PRE_INCREMENT(ptr) (++(*ptr))

// Macro for post-increment using bitwise operators


#define POST_INCREMENT(ptr) ((*ptr)++)

int main() {
int num = 5;

printf("Initial value: %d\n", num);

// Implementing pre-increment using the macro


PRE_INCREMENT(&num);
printf("After pre-increment: %d\n", num);

// Implementing post-increment using the macro


POST_INCREMENT(&num);
printf("After post-increment: %d\n", num);

return 0;
}
--------------------------------------------
#include <stdio.h>

// Function to count set bits in an integer


int countSetBits(int n) {
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}

// Function to count the number of bits needed to be flipped


int countBitsToFlip(int a, int b) {
// XOR a and b to find the differing bits
int xorResult = a ^ b;

// Count the set bits in the XOR result


return countSetBits(xorResult);
}

int main() {
int a, b;

// Input two numbers


printf("Enter the first number (a): ");
scanf("%d", &a);
printf("Enter the second number (b): ");
scanf("%d", &b);

// Calculate and print the number of bits needed to be flipped


int flips = countBitsToFlip(a, b);
printf("Number of bits needed to be flipped: %d\n", flips);

return 0;
}

#include <stdio.h>

// Macro to count set bits in an integer


#define COUNT_SET_BITS(n) \
({ \
int count = 0; \
while (n) { \
count += n & 1;\
n >>= 1; \
} \
count; \
})

// Macro to count the number of bits needed to be flipped


#define COUNT_BITS_TO_FLIP(a, b) COUNT_SET_BITS((a) ^ (b))

int main() {
int a, b;

// Input two numbers


printf("Enter the first number (a): ");
scanf("%d", &a);
printf("Enter the second number (b): ");
scanf("%d", &b);

// Calculate and print the number of bits needed to be flipped using the macro
int flips = COUNT_BITS_TO_FLIP(a, b);
printf("Number of bits needed to be flipped: %d\n", flips);

return 0;

#include <stdio.h>

// Macro to set n bits of a number with the n bits of another number from specified
position
#define SET_BITS_FROM_POSITION(num, num2, pos, n) \
({ \
int mask = ((1 << (n)) - 1) << pos; \
num = (num & ~mask) | ((num2 << pos) & mask); \
})

// Function to print binary representation of a number


void printBinary(int num) {
printf("Binary Representation: ");
for (int i = sizeof(num) * 8 - 1; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}
int main() {
int num, num2, pos, n;

// Input the numbers and position from the user


printf("Enter the first number (num): ");
scanf("%d", &num);
printf("Enter the second number (num2): ");
scanf("%d", &num2);
printf("Enter the starting position: ");
scanf("%d", &pos);
printf("Enter the number of bits to set: ");
scanf("%d", &n);

// Print the original numbers


printf("Original num: %d\n", num);
printf("Original num2: %d\n", num2);

// Set n bits of num with the n bits of num2 from specified position
SET_BITS_FROM_POSITION(num, num2, pos, n);

// Print the result


printf("Result after setting bits: %d\n", num);

// Print binary representation of the result


printBinary(num);

return 0;
}

You might also like