Skip to content

Instantly share code, notes, and snippets.

@scriptpapi
Created May 23, 2018 21:29
Show Gist options
  • Save scriptpapi/45b20ddcc0f784ac6ae454610b82f216 to your computer and use it in GitHub Desktop.
Save scriptpapi/45b20ddcc0f784ac6ae454610b82f216 to your computer and use it in GitHub Desktop.
basic bitwise operations
// some bitwise operations
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned char a = 5;
unsigned char b = 9;
printf("a= %d, b= %d\n", a, b);
printf("binary a= 00000101, b= 00001001\n");
printf("a&b= %d, binary= 00000001\n", a & b); // AND
printf("a|b= %d, binary= 00001101\n", a | b); // OR
printf("a^b= %d, binary= 00001100\n", a ^ b); // XOR
printf("~a = %d, binary= 11111010\n", ~a); // NOT A
printf("~b = %d, binary= 11110110\n", ~b); // NOT A
printf("a<<1 = %d, binary=00001010 \n", a << 1); // Left shift A
printf("a>>1 = %d, binary=00000010 \n", a >> 1); // Right shift A
printf("b<<1 = %d, binary= 00010010\n", b << 1); // Left shift B
printf("b>>1 = %d, binary= 00000100\n", b >> 1); // Right shift B
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment