#include <stdio.h>

void printBinary(int val, int noBits){
    for(int i=noBits-1; i>=0; i--){
        printf("%d",(val>>i)&1);
    }
    printf("\n");
}

int main() {
    int a = 0b1010;
    int b = a;
    int count = 0;
    int co = 0;
    while(b!=0){
        if((b&1)==1){ // why not (a==1)
            co++;   
        }
        count++;
        b=b>>1;
    }
    printf("%d\n",co);

    printf("Binary representation before XOR: ");
    printBinary(a, count);

    a = a ^ 0b1111; // XOR with 0b1111 to toggle all bits

    printf("Binary representation after XOR: ");
    printBinary(a, count);

    return 0;
}

/*

Let's assume a is a 4-bit binary number: a = 0b1010.

if (a == 1):

This condition checks if the entire value of a is equal to 1.

c
Copy code
int a = 0b1010; // Binary representation: 1010
if (a == 1) {
    // This condition will be false because 'a' is not equal to 1 as a whole.
}
In this case, the value of a is 10 in decimal, not 1.

if ((a & 1) == 1):

This condition uses bitwise AND (&) with 1 to check if the least significant bit (LSB) of a is 1.

c
Copy code
int a = 0b1010; // Binary representation: 1010
if ((a & 1) == 1) {
    // This condition will be true because the LSB of 'a' is 1.
}
The bitwise AND operation (a & 1) isolates the LSB of a, and the condition checks if that isolated
bit is 1. In this case, the LSB of a is 0b1010, so the condition is true.

In summary, if (a == 1) checks if the entire value of a is 1, while if ((a & 1) == 1) checks if the
least significant bit of a is 1. For the binary number 1010, the latter condition would be true,
as it checks the individual bits.

*/

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: