0% found this document useful (0 votes)
2 views2 pages

Bitwise C Programs

The document contains several C programming examples demonstrating different concepts. It includes swapping numbers using bitwise operators, returning multiple values using structures, reversing bits, isolating bits with bit masking, and using enums, unions, and structures to manage item types. Each example is accompanied by code snippets and explanations of their functionality.

Uploaded by

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

Bitwise C Programs

The document contains several C programming examples demonstrating different concepts. It includes swapping numbers using bitwise operators, returning multiple values using structures, reversing bits, isolating bits with bit masking, and using enums, unions, and structures to manage item types. Each example is accompanied by code snippets and explanations of their functionality.

Uploaded by

ukseducation2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Swap two numbers using bitwise operators


-------------------------------------------
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

2. Return 3 numbers from a function using a structure


-----------------------------------------------------
#include <stdio.h>
struct Numbers {
int x, y, z;
};
struct Numbers getNumbers() {
struct Numbers nums = {10, 20, 30};
return nums;
}
int main() {
struct Numbers result = getNumbers();
printf("x = %d, y = %d, z = %d\n", result.x, result.y, result.z);
return 0;
}

3. Reverse an integer using bitwise operators


---------------------------------------------
#include <stdio.h>
unsigned int reverseBits(unsigned int num) {
unsigned int rev = 0;
for (int i = 0; i < 32; i++) {
rev <<= 1;
rev |= (num & 1);
num >>= 1;
}
return rev;
}
int main() {
unsigned int n = 25;
printf("Reversed bits = %u\n", reverseBits(n));
return 0;
}

4. Bit masking to isolate bits


------------------------------
#include <stdio.h>
int main() {
unsigned int value = 0b11010110;
unsigned int mask = 0b00001111;
unsigned int result = value & mask;
printf("Isolated bits = %u\n", result);
return 0;
}

5. Enum, union, and structure for items


---------------------------------------
#include <stdio.h>
#include <string.h>
enum ItemType { FOOD, ELECTRONICS };
union ItemDetail {
char expiry[20];
int power;
};
struct Item {
enum ItemType type;
union ItemDetail detail;
};
int main() {
struct Item items[2];
items[0].type = FOOD;
strcpy(items[0].detail.expiry, "2025-12-31");
items[1].type = ELECTRONICS;
items[1].detail.power = 150;
for (int i = 0; i < 2; i++) {
if (items[i].type == FOOD) {
printf("Item %d is FOOD, Expiry: %s\n", i + 1, items[i].detail.expiry);
} else {
printf("Item %d is ELECTRONICS, Power: %dW\n", i + 1,
items[i].detail.power);
}
}
return 0;
}

You might also like