Arjun Suresh (talk | contribs) (Created page with "<metadesc>Bitwise Operators in C</metadesc> ==Bitwise AND, OR, EXOR== <syntaxhighlight lang="c" name="bitwise"> #include<stdio.h> int main() { int a; printf("Enter a n...") |
Arjun Suresh (talk | contribs) |
||
Line 33: | Line 33: | ||
printf("Enter a number and a power of 2\n"); | printf("Enter a number and a power of 2\n"); | ||
scanf("%d %d", &a, &b); | scanf("%d %d", &a, &b); | ||
− | printf("%d multiplied by %d is\n", a, a << b); | + | printf("%d multiplied by 2^%d is %d\n", a, b, a << b); |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 45: | Line 45: | ||
printf("Enter a number and a power of 2\n"); | printf("Enter a number and a power of 2\n"); | ||
scanf("%d %d", &a, &b); | scanf("%d %d", &a, &b); | ||
− | printf("%d divided by %d is\n", a, a >> b); | + | printf("%d divided by 2^%d is %d\n", a, b, a >> b); |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
<syntaxhighlight lang="c" name="bitwise">
int main() {
int a; printf("Enter a number\n"); scanf("%d", &a); printf("The third bit (from right) of %d is made 0 and we get %d\n", a, a& (-1<<3 | 11)); // -1 has all bits 1, so -1<<3 will have the rightmost 3 bits 0 and rest 1, and -1<<3 | 11 will have the 3rd bit from right as 0 and all other bits 1
} </syntaxhighlight>
<syntaxhighlight lang="c" name="bitwisenot">
int main() {
unsigned int a; printf("Enter a number\n"); scanf("%u", &a); printf("Negation of %u is %u\n", a, ~a); //~a will have all bits of a inverted (0's become 1 and 1's become 0)
} </syntaxhighlight>
<syntaxhighlight lang="c" name="leftshift">
int main() {
int a, b; printf("Enter a number and a power of 2\n"); scanf("%d %d", &a, &b); printf("%d multiplied by 2^%d is %d\n", a, b, a << b);
} </syntaxhighlight>
<syntaxhighlight lang="c" name="rightshift">
int main() {
int a, b; printf("Enter a number and a power of 2\n"); scanf("%d %d", &a, &b); printf("%d divided by 2^%d is %d\n", a, b, a >> b);
} </syntaxhighlight>
<syntaxhighlight lang="c" name="bitwise">
int main() {
int a; printf("Enter a number\n"); scanf("%d", &a); printf("The third bit (from right) of %d is made 0 and we get %d\n", a, a& (-1<<3 | 11)); // -1 has all bits 1, so -1<<3 will have the rightmost 3 bits 0 and rest 1, and -1<<3 | 11 will have the 3rd bit from right as 0 and all other bits 1
} </syntaxhighlight>
<syntaxhighlight lang="c" name="bitwisenot">
int main() {
unsigned int a; printf("Enter a number\n"); scanf("%u", &a); printf("Negation of %u is %u\n", a, ~a); //~a will have all bits of a inverted (0's become 1 and 1's become 0)
} </syntaxhighlight>
<syntaxhighlight lang="c" name="leftshift">
int main() {
int a, b; printf("Enter a number and a power of 2\n"); scanf("%d %d", &a, &b); printf("%d multiplied by 2^%d is %d\n", a, b, a << b);
} </syntaxhighlight>
<syntaxhighlight lang="c" name="rightshift">
int main() {
int a, b; printf("Enter a number and a power of 2\n"); scanf("%d %d", &a, &b); printf("%d divided by 2^%d is %d\n", a, b, a >> b);
} </syntaxhighlight>