Sunteți pe pagina 1din 12

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.

Many situation, need to operate on the bits of a data word


Register inputs or outputs Controlling attached devices Obtaining status

Bitwise Operations in Integers


Corresponding bits of both operands are combined by the usual logic operations.

& 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

<< Shift left


Multiply by 2

^ Exclusive OR
Result is 1 if operand bits are different

>> Shift right


Divide by 2

Left and Right Shift Operators


The >> operator shifts a variable to the right and the << operator shifts a variable to the left. Zeros are shifted into vacated bits, but with signed data types, what happens with sign bits is platform dependant.
The number of bit positions these operators shift the value on their left is specified on the right of the operator. Uses include fast multiplication or division of integers by integer powers of 2, e.g. 2,4,8,16 etc.

Left and right shift example


#include <stdio.h> int main(void){ unsigned int a=16; printf("%d\t",a>>3); /* prints 16 divided by 8 */ printf("%d\n",a<<3); /* prints 16 multiplied by 8 */ return 0; } output: 2 128

Bitwise AND and inclusive OR


Single & and | operators (bitwise AND and OR) work differently from logical AND and OR ( && and || ). You can think of the logical operators as returning a single 1 for true, and 0 for false. The purpose of the & and | bitwise operators is to return a resulting set of output 1s and 0s based on the boolean AND or OR operations between corresponding bits of the input.

Bitwise AND and inclusive OR


Truth Table Bitwise AND (&) Input A 0 0 1 1 B 0 1 0 1 Output C 0 0 0 1 A 0 0 1 1 Truth Table Bitwise OR (|) Input B 0 1 0 1 Output C 0 1 1 1

Example: a=28, b=0


A & B 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 1 0 | | A B 0 0 0

Example: a=28, b=32 0 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 0

C
A & B C

C
A B C

Example: a=28, b=255 0 0 0

Example: a=28, b=29 0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 0 0 1 1

Bitwise AND/OR example


#include <stdio.h> int main(void){ unsigned char a='\x00',b='\xff',c; c='\x50' | '\x07'; /* 01010000 | 00000111 */ printf("hex 50 | 07 is %x\n",c); c='\x73' & '\x37'; /* 01110011 & 00110111 */ printf("hex 73 & 37 is %x\n",c); return 0; } Output: hex 50 | 07 is 57 hex 73 & 37 is 33

Bitwise Exclusive OR (^)


Truth Table Bitwise Exclusive OR (^) Input A 0 0 1 1 B 0 1 0 1 Output C 0 1 1 0

#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

Example: a=5, b=10


a ^ b 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 1 1 0

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

One's complement operator


Symbol: ~
This is a unary operator in the sense that it works on a single input value. The bit pattern output is the opposite of the bit pattern input with input 1s becoming output 0s and input 0s becoming output 1s.
Example: a=23; ~a=232; a ~a 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0

Used in Binary Subtraction, May be used for simple encryption

Passing Command Line Arguments


When you execute a program you can include arguments on the command line. The run time environment will create an argument vector. argv is the argument vector argc is the number of arguments Argument vector is an array of pointers to strings. a string is an array of characters terminated by a binary 0 (NULL or \0). argv[0] is always the program name, so argc is at least 1.
C:\>try g 2 fred

argc = 4, argv = <address0>

try\0 [0] [1] [2] [3] [4] argv: <addres1> <addres2> <addres3> <addres4> NULL -g\0 2\0

fred\0

Passing Command Line Arguments


/* Example Program add.c */ #include <stdio.h> #include <stdlib.h> #include <conio.h> int main(int argc, char *argv[]) { int a, b; a=(atoi)(argv[1]); b=(atoi)(argv[2]); printf("\n\n\t\t\t%d\n\n", a+b); getch(); return 0; }
C:\>add 10 5

argc = 3, argv = <address0>

add\0 [0] [1] [2] [3] argv: <addres1> <addres2> <addres3> NULL 10\0 5\0

Passing Command Line Arguments


/* Example Program filecopy.c */ #include <stdio.h> #include <stdlib.h> #include <conio.h>
C:\>filecopy srcfile.c targfile.c

int main(int argc, char *argv[]) { FILE *fs, *ft; char ch; . }

S-ar putea să vă placă și