-
-
Save davidlares/89551d78cd1b69353bd5e7ad23a92258 to your computer and use it in GitHub Desktop.
Bit-wise Left Shift (<<) operator example in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// getting the binary representation - 16 bit format | |
void showbits(int number) { | |
int i, k, andmask; | |
for(i = 15; i >= 0; i--) { | |
andmask = 1 << i; | |
k = andmask & number; | |
k == 0? printf("0") : printf("1"); | |
} | |
} | |
int main() { | |
// starting values | |
int input = 5225; | |
int k; | |
// iterating a J variable | |
for(int j = 0; j < 3; j++) { | |
// performing right-shift to the J variable once 0-1-2 times | |
k = input << j; | |
printf("\n Binary rep of number of %d is: " , input); | |
showbits(k); // binary | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment