This blog is under construction

Sunday 14 July 2013

C program to set, reset, check, clear and toggle a bit

Write a C program to set, reset, check, clear and toggle a bit.

How to set a bit?
value = data | (1 << n)). Where data is the input value and n be bit to be set.
Note:  Bits starts from 0 to 7 (0000 0000).  Least Significant Bit is the 0th bit.

Example: Input data is 8
Bit needs to be set is 2
value = (0000 1000) | (0000 0001 << 2) => 0000 1100 => 12


How to check a bit is set or unset?
value = data & (1 << n)

Example: Input data is 8
Bit needs to be checked is 3
value = (0000 1000) | (0000 0001 << 3) => 1
Third bit set in the given input value.

How to clear a bit?
value = data &  ~(1 << n)

Example: Input data is 8
Bit needs to cleared is 3
value = (0000 1000) &  (~(0000 0001 << 3))
         = (0000 1000) &  (~(0000 1000))
         = (0000 1000) & (1111 0111) = 0


How to toggle a bit?
value = data ^ (1 << n)

Example: Input data is 8
Bit needs to be toggled is 2.
value = (0000 1000) ^ (0000 0001 << 2)
         = (0000 1000) ^ (0000 0100) => (0000 1100) = 12



  #include <stdio.h>
  #include <stdlib.h>

  int main() {
        int ch, n, input;
        while (1) {
                printf("\nNote: 1 byte = 8 bits(0 - 7)\n");
                printf("1. Set nth Bit\t2. Reset nth Bit\n");
                printf("3. Check a Bit\t4. Clear nth Bit\n");
                printf("5. Toggle a Bit\t6. Exit\n");
                printf("Enter your choice:");
                scanf("%d", &ch);
                if (ch != 6) {
                        printf("Enter your input value:");
                        scanf("%d", &input);
                        printf("Enter the value for n(nth bit):");
                        scanf("%d", &n);
                }
                switch(ch) {
                        case 1:
                                /* setting nth bit */
                                input = input | (1 << n);
                                printf("Result: %d\n", input);
                                break;
                        case 2:
                                /* reset operation */
                                input = input & (~0);
                                printf("Result: %d\n", input);
                                break;
                        case 3:
                                /* check a bit is set or not */
                                input = input & (1 << n);
                                printf("Bit %d is %s\n", n, input?"set":"unset");
                                break;
                        case 4:
                                /* clear nth bit */
                                input = input & (~(1 << n));
                                printf("Result: %d\n", input);
                                break;
                        case 5:
                                /* toggle a bit */
                                input = input ^ (1 << n);
                                printf("Result: %d\n", input);
                                break;
                        case 6:
                                exit(0);
                        default:
                                printf("Wrong Option!!\n");
                                break;
                }

        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Note: 1 byte = 8 bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:1
  Enter your input value:8
  Enter the value for n(nth bit):2
  Result: 12

  Note: 1 byte = 8 bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:2
  Enter your input value:8
  Enter the value for n(nth bit):3
  Result: 8

  Note: 1 byte = 8bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:3
  Enter your input value:8
  Enter the value for n(nth bit):3
  Bit 3 is set

  Note: 1 byte = 8 bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:4
  Enter your input value:8
  Enter the value for n(nth bit):3
  Result: 0

  Note: 1 byte = 8 bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:5
  Enter your input value:8
  Enter the value for n(nth bit):2
  Result: 12

  Note: 1 byte = 8 bits(0 - 7)
  1. Set nth Bit 2. Reset nth Bit
  3. Check a Bit 4. Clear nth Bit
  5. Toggle a Bit 6. Exit
  Enter your choice:6


1 comment:

  1. please correct below Example -:
    Example: Input data is 8
    Bit needs to be checked is 3
    value = (0000 1000) | (0000 0001 << 3) => 1
    Third bit set in the given input value.

    ReplyDelete