Skip to content

Instantly share code, notes, and snippets.

@davidlares
Created February 14, 2020 21:09
Show Gist options
  • Save davidlares/89551d78cd1b69353bd5e7ad23a92258 to your computer and use it in GitHub Desktop.
Save davidlares/89551d78cd1b69353bd5e7ad23a92258 to your computer and use it in GitHub Desktop.
Bit-wise Left Shift (<<) operator example in C
#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