How to use the operator ~ (Tilde) that inverse (flips) all the bits of its operand (a number) in C++

1 Answer

0 votes
#include <iostream>

void print_bits(unsigned int n, const int total_bits) { 
    for (int i = total_bits; i >= 0; i--) {
        std::cout << ((n >> i) & 1);
    }
}

int main(void)
{
    int n = 3;
    printf(" %d = ", n);
    print_bits(n, 8);
     
    n = ~n;
    printf("\n%d = ", n);
    print_bits(n, 8);
}

 
 
/*
run:
 
 3 = 000000011
-4 = 111111100

*/

 



answered Nov 21, 2024 by avibootz
...