Bit Operators (& and, | or, ^ Xor)

 

a.c
main()
{
printf("%d\n",4 & 5);
}
 
Output
4
 

  8   4    2   1

0

1

0

0

 = 4

 

  8   4    2   1

0

1

0

1

 = 5

 

---------------------

  8   4    2   1

0

1

0

0

 = 4

 

 

 
a.c
main()
{
printf("%d\n", 4 | 2);
}
 
Output
6
 

 

  8   4    2   1

0

1

0

0

 = 4

 

  8   4    2   1

0

0

1

0

 = 2

 

---------------------

  8    4    2   1

0

1

1

0

 = 6

 

 

 
a.c
main()
{
printf("%d\n",4^2);
printf("%d\n",4^6);
printf("%d\n",2^6);
}
 
Output
6
2
4
 

 

  8   4    2   1

0

1

0

0

 = 4

 

  8   4    2   1

0

0

1

0

 = 2

 

---------------------

  8    4    2   1

0

1

1

0

 = 6

 

 

 

  8   4    2   1

0

1

0

0

 = 4

 

  8    4    2   1

0

1

1

0

  = 6

 

---------------------

  8   4    2   1

0

0

1

0

  = 2

 

 

 

  8   4    2   1

0

0

1

0

  = 2

 

  8    4    2   1

0

1

1

0

  = 6

 

---------------------

  8   4    2   1

0

1

0

0

  = 4

 

 

 

 

encrypt-decrypt.c
 
//   Usage: encrypt-decrypt <sorc.file> <dest.file> <password>
 
#include<stdio.h>
main(int argc, char *argv[])  
{
FILE *fpi,*fpo; 
char *p; int i;
p=argv[3];
fpi=fopen(argv[1],"rb");
fpo=fopen(argv[2],"wb");
       while((i=fgetc(fpi))!=-1)
       {
               i=i^*p;
               p++;
               if(*p==0)
               p=argv[3];
               fputc(i,fpo);
       }
}

 

C:\> encrypt-decrypt Orignal.doc Encrypt.doc password
 
C:\> encrypt-decrypt Encrypt.doc Orignal.doc password
 
Back to the main page