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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,788 answers

573 users

How to double a number using binary shift left operation in C

1 Answer

0 votes
#include <stdio.h>

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(void)
{
    int n = 7;

    printf("n = %d\n", n);
    print_bits(n, 4);
    printf("\n");

    n = n << 1; // double the number
    printf("n = %d\n", n);
    print_bits(n, 4);

    return 0;
}




/*
run:

n = 7
0111
n = 14
1110

*/

 





answered Jul 7, 2023 by avibootz
edited Jul 7, 2023 by avibootz

Related questions

1 answer 29 views
2 answers 116 views
1 answer 63 views
...