Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to use ~ (tilde) operator in C

1 Answer

0 votes
#include <stdio.h>

/*
The ~ (tilde) operator performs a bitwise NOT operation - 
all the 1 bits are set to 0 and all the 0 bits are set to 1
creates the complement of a number
*/

void print_bits(int n, int size) {
    for (int i = 1 << (size - 1); i > 0; i = i / 2) {
        (n & i) ? printf("1") : printf("0");
    }
}

int main() {
    int n = 43241;
    print_bits(n , 16);
    
    printf("\n");
    
    n = ~n;
    print_bits(n , 16);

    return 0;
}



/*
run:

1010100011101001
0101011100010110

*/

 



answered Jun 7, 2024 by avibootz

Related questions

1 answer 180 views
2 answers 144 views
144 views asked Nov 1, 2021 by avibootz
2 answers 134 views
134 views asked Nov 1, 2021 by avibootz
2 answers 121 views
121 views asked Nov 1, 2021 by avibootz
...