Bitwise Operations
Bitwise Operations
'C' was designed to write system software as an alternative to assembler: compilers, kernels, device drivers, interpreters, relational database engines, virtual machines. So this language needs access to raw hardware and individual bit values. Coding designed for specific hardware design features will be non portable.
Different microprocessors organise integer and floating point data differently, e.g. two's or one's complement, location and size of exponent, sign bit and mantissa.
Device drivers for different hardware implement different instruction sets.
& AND
Result is 1 if both operand bits are 1
~ Complement
Each bit is reversed
| OR
Result is 1 if either operand bit is 1
^ Exclusive OR
Result is 1 if operand bits are different
C
A & B C
C
A B C
#include<stdio.h> int main() { int a=5, b=10; a=a^b; b=a^b; a=a^b; printf("a=%d and b=%d\n", a, b); return 0; }
1 0 1 0 1
c
^ b a
May be used for encryption with a security key a=Data b=Security key of encryption c=a^b is encrypted data a=a^b^b unencrypted data
try\0 [0] [1] [2] [3] [4] argv: <addres1> <addres2> <addres3> <addres4> NULL -g\0 2\0
fred\0
add\0 [0] [1] [2] [3] argv: <addres1> <addres2> <addres3> NULL 10\0 5\0
int main(int argc, char *argv[]) { FILE *fs, *ft; char ch; . }