1
1
h>
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");
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
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");
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;
default:
printf("Invalid choice. Please try again.\n");
}
} else {
printf("ERROR: Please pass a valid choice.\n");
}
}
return 0;
}
#include <stdio.h>
int main() {
unsigned int num, result;
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>
int main() {
unsigned int num, result;
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
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;
return 0;
}
#include <stdio.h>
int main() {
unsigned int num, num2, pos, 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>
int main() {
unsigned int num, num2, pos, n, result;
return 0;
}
#include <stdio.h>
int main() {
unsigned int num, num2, pos, n, result;
return 0;
}
#include <stdio.h>
int main() {
unsigned int num, num2, pos, 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)))
int main() {
unsigned int num, num2, pos, n, result;
return 0;
}
#include <stdio.h>
int main() {
unsigned int num;
return 0;
}
#include <stdio.h>
#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;
return 0;
}
#include <stdio.h>
return 0;
}
int main() {
int num = 5;
return 0;
}
----------------------------------------------------------
#include <stdio.h>
int main() {
int num = 5;
return 0;
}
--------------------------------------------
#include <stdio.h>
int main() {
int a, b;
return 0;
}
#include <stdio.h>
int main() {
int a, 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); \
})
// Set n bits of num with the n bits of num2 from specified position
SET_BITS_FROM_POSITION(num, num2, pos, n);
return 0;
}