C Tutorial/Operator/Bitwise Operator
Содержание
A special feature of the >> and
#include <stdio.h>
int main(){
int x, y = 6;
x = y >> 1;
printf("%d",x);
x = y/2;
printf("%d",x);
}
Bitwise and: c1 & c2
# include<stdio.h>
main()
{
char c1 = 4,c2 = 6,c3 = 3;
c3 = c1 & c2;
printf("\n Bitwise AND i.e. c1 & c2 = %d",c3);
}
Bitwise operator
There are six bit operators:
- bitwise AND(&)
- bitwise OR(|)
- bitwise XOR(^)
- bitwise complement(~)
- left shift(<<)
- right shift(>>)
# include<stdio.h>
main()
{
char c1 = 1,c2 = 2,c3 = 3;
c3 = c1 & c2;
printf("\n Bitwise AND i.e. c1 & c2 = %c",c3);
c3 = c1 | c2;
printf("\n Bitwise OR i.e. c1 | c2 = %c",c3);
c3 = c1 ^ c2;
printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3);
c3 = ~c1;
printf("\n ones complement of c1 = %c",c3);
c3 = c1<<2;
printf("\n left shift by 2 bits c1 << 2 = %c",c3);
c3 = c1>>2;
printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
}
Bitwise AND i.e. c1 & c2 = Bitwise OR i.e. c1 | c2 = Bitwise XOR i.e. c1 ^ c2 = ones complement of c1 = ? left shift by 2 bits c1 << 2 = right shift by 2 bits c1 >> 2 =
Bitwise or: c1 | c2
# include<stdio.h>
main()
{
char c1 = 4,c2 =6 ,c3 = 3;
c3 = c1 | c2;
printf("\n Bitwise OR i.e. c1 | c2 = %c",c3);
}
Bitwise XOR: c1 ^ c2
# include<stdio.h>
main()
{
char c1 = 4,c2 = 6,c3 = 3;
c3 = c1 ^ c2;
printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3);
}
Complement: ~
# include<stdio.h>
main()
{
char c1 = 4,c2 = 6,c3 = 3;
c3 = ~c1;
printf("\n ones complement of c1 = %c",c3);
}
Left shift operator
# include<stdio.h>
main()
{
char c1 = 1,c2 = 2,c3 = 3;
c3 = c1<<2;
printf("\n left shift by 2 bits c1 << 2 = %c",c3);
}
Right shift operation
# include<stdio.h>
main()
{
char c1 = 1,c2 = 2,c3 = 3;
c3 = c1>>2;
printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
}
To divide value by 4
#include <stdio.h>
int main(){
int x, y = 8;
x = y >> 2;
printf("%d",x);
x = y/4;
printf("%d",x);
}