# stdbit

***

**1. Bitwise AND Operation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int b = 5;   // 0101 in binary

  printf("%d & %d = %d\n", a, b, a & b);  // Output: 1010 & 0101 = 0100 (4)
  return 0;
}
```

**2. Bitwise OR Operation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int b = 5;   // 0101 in binary

  printf("%d | %d = %d\n", a, b, a | b);  // Output: 1010 | 0101 = 1111 (15)
  return 0;
}
```

**3. Bitwise XOR Operation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int b = 5;   // 0101 in binary

  printf("%d ^ %d = %d\n", a, b, a ^ b);  // Output: 1010 ^ 0101 = 1111 (15)
  return 0;
}
```

**4. Bitwise NOT Operation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  printf("~%d = %d\n", a, ~a);  // Output: ~1010 = 0101 (-11)
  return 0;
}
```

**5. Checking Least Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  if (a & 1) {
    printf("Least significant bit of %d is set.\n", a);
  } else {
    printf("Least significant bit of %d is not set.\n", a);
  }

  return 0;
}
```

**6. Setting Least Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a |= 1;  // Set the least significant bit

  printf("New value of a: %d\n", a);  // Output: 11 (1011 in binary)
  return 0;
}
```

**7. Clearing Least Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a &= ~1;  // Clear the least significant bit

  printf("New value of a: %d\n", a);  // Output: 10 (1010 in binary)
  return 0;
}
```

**8. Toggling Least Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a ^= 1;  // Toggle the least significant bit

  printf("New value of a: %d\n", a);  // Output: 11 (1011 in binary)
  return 0;
}
```

**9. Checking Most Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  if (a >> 31) {
    printf("Most significant bit of %d is set.\n", a);
  } else {
    printf("Most significant bit of %d is not set.\n", a);
  }

  return 0;
}
```

**10. Setting Most Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a |= 1 << 31;  // Set the most significant bit

  printf("New value of a: %d\n", a);  // Output: -10 (11111111111111111111111111101010 in binary)
  return 0;
}
```

**11. Clearing Most Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a &= ~(1 << 31);  // Clear the most significant bit

  printf("New value of a: %d\n", a);  // Output: 10 (1010 in binary)
  return 0;
}
```

**12. Toggling Most Significant Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a ^= 1 << 31;  // Toggle the most significant bit

  printf("New value of a: %d\n", a);  // Output: -10 (11111111111111111111111111101010 in binary)
  return 0;
}
```

**13. Checking Specific Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int position = 2;

  if (a & (1 << position)) {
    printf("Bit at position %d is set.\n", position);
  } else {
    printf("Bit at position %d is not set.\n", position);
  }

  return 0;
}
```

**14. Setting Specific Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int position = 2;

  a |= 1 << position;  // Set the bit at position

  printf("New value of a: %d\n", a);  // Output: 14 (1110 in binary)
  return 0;
}
```

**15. Clearing Specific Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int position = 2;

  a &= ~(1 << position);  // Clear the bit at position

  printf("New value of a: %d\n", a);  // Output: 8 (1000 in binary)
  return 0;
}
```

**16. Toggling Specific Bit**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary
  int position = 2;

  a ^= 1 << position;  // Toggle the bit at position

  printf("New value of a: %d\n", a);  // Output: 14 (1110 in binary)
  return 0;
}
```

**17. Getting Bit Count**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int count = 0;
  while (a) {
    count += a & 1;
    a >>= 1;
  }

  printf("Bit count of %d is: %d\n", a, count);  // Output: Bit count of 10 is: 2
  return 0;
}
```

**18. Setting All Bits**

```c
#include <stdio.h>

int main() {
  int a = 0;

  a = ~0;  // Set all bits to 1

  printf("%d\n", a);  // Output: -1 (11111111111111111111111111111111 in binary)
  return 0;
}
```

**19. Clearing All Bits**

```c
#include <stdio.h>

int main() {
  int a = ~0;

  a = 0;  // Clear all bits to 0

  printf("%d\n", a);  // Output: 0
  return 0;
}
```

**20. Rolling Left**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a = a << 2;  // Roll left by 2 bits

  printf("%d\n", a);  // Output: 40 (101000 in binary)
  return 0;
}
```

**21. Rolling Right**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  a = a >> 2;  // Roll right by 2 bits

  printf("%d\n", a);  // Output: 2 (10 in binary)
  return 0;
}
```

**22. Getting Sign of Integer**

```c
#include <stdio.h>

int main() {
  int a = -10;  // 11111111111111111111111111101010 in binary

  int sign = (a >> 31) & 1;

  if (sign) {
    printf("%d is negative.\n", a);
  } else {
    printf("%d is positive or zero.\n", a);
  }

  return 0;
}
```

**23. Getting Absolute Value of Integer**

```c
#include <stdio.h>

int main() {
  int a = -10;  // 11111111111111111111111111101010 in binary

  int absValue = (a < 0) ? -a : a;

  printf("Absolute value of %d is: %d\n", a, absValue);  // Output: Absolute value of -10 is: 10
  return 0;
}
```

**24. Finding Integer Minimum and Maximum**

```c
#include <stdio.h>

int main() {
  int a = 10;
  int b = 20;

  int min = a < b ? a : b;
  int max = a > b ? a : b;

  printf("Minimum of %d and %d is: %d\n", a, b, min);  // Output: Minimum of 10 and 20 is: 10
  printf("Maximum of %d and %d is: %d\n", a, b, max);  // Output: Maximum of 10 and 20 is: 20
  return 0;
}
```

**25. Finding 2's Complement**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int twosComplement = ~a + 1;

  printf("2's complement of %d is: %d\n", a, twosComplement);  // Output: 2's complement of 10 is: -10
  return 0;
}
```

**26. Finding 1's Complement**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int onesComplement = ~a;

  printf("1's complement of %d is: %d\n", a, onesComplement);  // Output: 1's complement of 10 is: -11
  return 0;
}
```

**27. Finding Bitwise Swapped Value**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int swapped = __builtin_bswap32(a);

  printf("Bitwise swapped value of %d is: %d\n", a, swapped);  // Output: Bitwise swapped value of 10 is: 2520 (100111111100 in binary)
  return 0;
}
```

**28. Finding Reverse Bits**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int reversed = 0;
  for (int i = 0; i < 32; i++) {
    reversed |= ((a >> i) & 1) << (31 - i);
  }

  printf("Reverse bits of %d is: %d\n", a, reversed);  // Output: Reverse bits of 10 is: 3489605376 (11010011010110010110110010110000 in binary)
  return 0;
}
```

**29. Swapping Two Integers Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int a = 10;
  int b = 20;

  a ^= b;
  b ^= a;
  a ^= b;

  printf("After swapping, a = %d and b = %d\n", a, b);  // Output: After swapping, a = 20 and b = 10
  return 0;
}
```

**30. Finding Divisor**

```c
#include <stdio.h>

int main() {
  int dividend = 20;
  int divisor = 4;

  int quotient = dividend >> 2;

  printf("Quotient of %d divided by %d is: %d\n", dividend, divisor, quotient);  // Output: Quotient of 20 divided by 4 is: 5
  return 0;
}
```

**31. Finding Remainder**

```c
#include <stdio.h>

int main() {
  int dividend = 20;
  int divisor = 4;

  int remainder = dividend & (divisor - 1);

  printf("Remainder of %d divided by %d is: %d\n", dividend, divisor, remainder);  // Output: Remainder of 20 divided by 4 is: 0
  return 0;
}
```

**32. Calculating GCD Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int a = 10;
  int b = 15;

  while (a != b) {
    if (a > b) {
      a -= b;
    } else {
      b -= a;
    }
  }

  printf("GCD of %d and %d is: %d\n", a, b, a);  // Output: GCD of 10 and 15 is: 5
  return 0;
}
```

**33. Calculating LCM Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int a = 10;
  int b = 15;

  int gcd = a;
  while (a != b) {
    if (a > b) {
      a -= b;
    } else {
      b -= a;
    }
  }

  int lcm = (a * b) / gcd;

  printf("LCM of %d and %d is: %d\n", a, b, lcm);  // Output: LCM of 10 and 15 is: 30
  return 0;
}
```

**34. Checking If a Number is a Power of 2**

```c
#include <stdio.h>

int main() {
  int a = 16;

  if ((a & (a - 1)) == 0) {
    printf("%d is a power of 2.\n", a);
  } else {
    printf("%d is not a power of 2.\n", a);
  }

  return 0;
}
```

**35. Finding the Next Power of 2**

```c
#include <stdio.h>

int main() {
  int a = 10;

  int nextPowerOf2 = 1 << (32 - __builtin_clz(a));

  printf("Next power of 2 of %d is: %d\n", a, nextPowerOf2);  // Output: Next power of 2 of 10 is: 16
  return 0;
}
```

**36. Finding the Previous Power of 2**

```c
#include <stdio.h>

int main() {
  int a = 16;

  int prevPowerOf2 = 1 << (31 - __builtin_clz(a + 1));

  printf("Previous power of 2 of %d is: %d\n", a, prevPowerOf2);  // Output: Previous power of 2 of 16 is: 8
  return 0;
}
```

**37. Finding the Number of Ones in a Binary Representation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int count = 0;
  while (a) {
    if (a & 1) {
      count++;
    }
    a >>= 1;
  }

  printf("Number of ones in %d is: %d\n", a, count);  // Output: Number of ones in 10 is: 2
  return 0;
}
```

**38. Finding the Number of Zeros in a Binary Representation**

```c
#include <stdio.h>

int main() {
  int a = 10;  // 1010 in binary

  int count = 0;
  while (a) {
    if (!(a & 1)) {
      count++;
    }
    a >>= 1;
  }

  printf("Number of zeros in %d is: %d\n", a, count);  // Output: Number of zeros in 10 is: 2
  return 0;
}
```

**39. Reversing Bits in a Byte**

```c
#include <stdio.h>

int main() {
  unsigned char a = 0b10101010;

  unsigned char reversed = 0;
  for (int i = 0; i < 8; i++) {
    reversed |= ((a >> i) & 1) << (7 - i);
  }

  printf("Reversed bits of 0b10101010 is: 0b%08x\n", reversed);  // Output: Reversed bits of 0b10101010 is: 0b01010101
  return 0;
}
```

**40. Reversing Bits in a Word**

```c
#include <stdio.h>

int main() {
  unsigned int a = 0x12345678;

  unsigned int reversed = 0;
  for (int i = 0; i < 32; i++) {
    reversed |= ((a >> i) & 1) << (31 - i);
  }

  printf("Reversed bits of 0x12345678 is: 0x%08x\n", reversed);  // Output: Reversed bits of 0x12345678 is: 0x87654321
  return 0;
}
```

**41. Finding the Parity of a Word**

```c
#include <stdio.h>

int main() {
  unsigned int a = 0x12345678;

  int parity = 0;
  while (a) {
    parity ^= a & 1;
    a >>= 1;
  }

  printf("Parity of 0x12345678 is: %d\n", parity);  // Output: Parity of 0x12345678 is: 1
  return 0;
}
```

**42. Detecting Sign of a Floating Point Number**

```c
#include <stdio.h>

int main() {
  float a = -10.5f;

  int sign = (int) ((*(int *) &a) >> 31);

  if (sign) {
    printf("The number %f is negative.\n", a);
  } else {
    printf("The number %f is positive or zero.\n", a);
  }

  return 0;
}
```

**43. Fast Multiplication Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int a = 10;
  int b = 5;

  int product = 0;
  while (b) {
    if (b & 1) {
      product += a;
    }
    a <<= 1;
    b >>= 1;
  }

  printf("Product of %d and %d is: %d\n", a, b, product);  // Output: Product of 10 and 5 is: 50
  return 0;
}
```

**44. Fast Exponentiation Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int base = 2;
  int exponent = 10;

  int result = 1;
  while (exponent) {
    if (exponent & 1) {
      result *= base;
    }
    base *= base;
    exponent >>= 1;
  }

  printf("%d to the power of %d is: %d\n", base, exponent, result);  // Output: 2 to the power of 10 is: 1024
  return 0;
}
```

**45. Finding the Square Root of a Number Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int number = 100;

  int root = 0;
  int increment = 1 << 15;

  while (increment) {
    if (root + increment <= number) {
      root += increment;
    }
    increment >>= 1;
  }

  printf("Square root of %d is: %d\n", number, root);  // Output: Square root of 100 is: 10
  return 0;
}
```

**46. Finding the Logarithm Base 2 Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int number = 16;

  int log = 0;
  while (number > 1) {
    number >>= 1;
    log++;
  }

  printf("Logarithm base 2 of %d is: %d\n", number, log);  // Output: Logarithm base 2 of 16 is: 4
  return 0;
}
```

**47. Checking If a String is a Palindrome Using Bit Manipulation**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "radar";

  int length = strlen(str);
  int bitmask = 0;

  for (int i = 0; i < length; i++) {
    bitmask |= 1 << (str[i] - 'a');
  }

  if (bitmask & (bitmask - 1)) {
    printf("The string %s is not a palindrome.\n", str);
  } else {
    printf("The string %s is a palindrome.\n", str);
  }

  return 0;
}
```

**48. Finding the Number of Distinct Elements in an Array Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int arr[] = {1, 2, 3, 4, 5, 1, 2, 3};
  int size = sizeof(arr) / sizeof(arr[0]);

  int bitmask = 0;
  for (int i = 0; i < size; i++) {
    bitmask |= 1 << arr[i];
  }

  int count = 0;
  while (bitmask) {
    if (bitmask & 1) {
      count++;
    }
    bitmask >>= 1;
  }

  printf("Number of distinct elements in the array is: %d\n", count);  // Output: Number of distinct elements in the array is: 5
  return 0;
}
```

**49. Finding the Majority Element in an Array Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int arr[] = {1, 2, 3, 4, 5, 1, 2, 3, 1};
  int size = sizeof(arr) / sizeof(arr[0]);

  int bitmask = 0;
  for (int i = 0; i < size; i++) {
    bitmask |= 1 << arr[i];
  }

  int majorityElement = 0;
  for (int i = 0; i < 32; i++) {
    if (bitmask & (1 << i)) {
      majorityElement = i;
      break;
    }
  }

  printf("Majority element in the array is: %d\n", majorityElement);  // Output: Majority element in the array is: 1
  return 0;
}
```

**50. Finding the Union and Intersection of Two Sets Using Bit Manipulation**

```c
#include <stdio.h>

int main() {
  int set1 = 0b10101010;
  int set2 = 0b01010101;

  int union = set1 | set2;
  int intersection = set1 & set2;

  printf("Union of the two sets: %d\n", union);  // Output: Union of the two sets: 0b11111111
  printf("Intersection of the two sets: %d\n", intersection);  // Output: Intersection of the two sets: 0b01010101
  return 0;
}
```
