Join our subscribers list to get the latest news, updates and special offers directly in your inbox
Overview
Macros Operations
Bitwise Operations
Functions
As Embedded C is an extension of C Language. It inherits all the fundamental operators of C. In C there are 6 bitwise operators. These operators Operate on Binary Level on the data can be seen in the below table :
#include int main() { int x = 0b10101110; /* binary representation of 174 */ /* Select the 4th bit using a bit mask and the bitwise AND operator */ int bit4 = (x & (1 << 3)) >> 3; printf("The 4th bit of x is %d\n", bit4); /* should print 1 */ return 0; }
x'
#include int main() { int x = 0b11011101; /* binary representation of 221 */ /* Clear the 2nd bit using a bit mask and the bitwise AND operator */ x &= ~(1 << 1); printf("x after clearing the 2nd bit: %d\n", x); /* should print 219 (0b11011011) */ return 0; }
#include int main() { int x = 0b01101001; /* binary representation of 105 */ /* Set the 3rd bit using a bit mask and the bitwise OR operator */ x |= (1 << 2); printf("x after setting the 3rd bit: %d\n", x); /* should print 109 (0b01101101) */ return 0; }
#include int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to set: "); scanf("%d", &n); num |= (1 << n); printf("The new number is: %d", num); return 0; }
#include int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to toggle: "); scanf("%d", &n); num ^= (1 << n); printf("The new number is: %d", num); return 0; }
#include int main() { int num, count = 0; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { if (num & 1) { count++; } num >>= 1; } printf("The number of set bits is: %d", count); return 0; }
#include int main() { int num, count = 0; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { if ((num & 1) == 0) { count++; } num >>= 1; } printf("The number of zero bits is: %d", count); return 0; }
#include int main() { int x = 10; /* Multiply x by 2 using left shift */ x = x << 1; printf("x after left shift: %d\n", x); /* should print 20 */ return 0; }
#include int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to check: "); scanf("%d", &n); if ((num >> n) & 1) { printf("The bit is set.\n"); } else { printf("The bit is not set.\n"); } return 0; }
#include int main() { int x = 10; /* Divide x by 2 using right shift */ x = x >> 1; printf("x after right shift: %d\n", x); /* should print 5 */ return 0; }
#include int main() { int x = 10; int y = 20; /* Swap x and y using XOR */ x = x ^ y; y = x ^ y; x = x ^ y; printf("x after XOR swap: %d\n", x); /* should print 20 */ printf("y after XOR swap: %d\n", y); /* should print 10 */ return 0; }
EmbeddedWala Jun 14, 2023 0 28.0K
EmbeddedWala Apr 27, 2023 0 25.4K
EmbeddedWala Apr 26, 2023 0 22.0K
EmbeddedWala Feb 15, 2024 0 21.7K
EmbeddedWala Aug 30, 2022 0 19.7K
EmbeddedWala Jun 19, 2022 0 5.4K
This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies Find out more here