Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1997-2009 Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.

Similar presentations


Presentation on theme: "Copyright © 1997-2009 Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus."— Presentation transcript:

1 Copyright © 1997-2009 Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus

2 Copyright © 1997-2009 Curt Hill Aside Before discussing bitwise operators a more fundamental item needs to be introduced or reviewed Binary is the base 2 number system It is how things are represented in computers If an item cannot be represented in base two it cannot be stored on a computer

3 Copyright © 1997-2009 Curt Hill Binary Base two number system Each digit is 0 or 1 and multiplied by a power of two Hence a byte with just one bit turned on is a power of two All numbers on a computer are represented in binary because of the hardware

4 Copyright © 1997-2009 Curt Hill 8 bit example: 1 1 1 1 1 x 2 0 = 1 1 x 2 1 = 2 1 x 2 2 = 4 1 x 2 3 = 8 1 x 2 4 = 16 1 x 2 5 = 32 1 x 2 6 = 64 1 x 2 7 = 128

5 Copyright © 1997-2009 Curt Hill Other bases Binary has some problems, the largest of which is that it is very bulky Decimal from binary is not an easy conversion –It also obscures structure Most manufacturers use another power of two base to show things –Octal – base 8 = 2 3 –Hexadecimal – base 16 = 2 4

6 Copyright © 1997-2009 Curt Hill Octal Base 8 has 8 digits, 0 – 7 and every place is raised to a power of 8 Conversion to and from binary is way too easy –Partition into groups of 3 bits from right to left –Convert the three bits to octal Preferred by certain manufacturers such as DEC and Intel

7 Copyright © 1997-2009 Curt Hill Binary – Octal Conversion Start with binary0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 Partition (right to left) in groups of 3 0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 Write digits0 6 7 1 3 5 How easy is that? Conversion of octal to binary is the reverse

8 Copyright © 1997-2009 Curt Hill Hexadecimal Base 16 has 16 digits, 0 – 9, A-F and every place is raised to a power of 16 –A=10, B=11, C=12, D=13, E=14, F=15 Conversion to and from binary is also easy –Partition into groups of 4 bits –Convert the four bits to hexadecimal Preferred by certain manufacturers such as IBM and Burroughs

9 Copyright © 1997-2009 Curt Hill Binary – Hexadecimal Conversion Start with binary0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 Partition (right to left) in groups of 4 0 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 Write digits 6 E 5 D How easy is that? Conversion of hexadecimal to binary is the reverse

10 Copyright © 1997-2009 Curt Hill C Notation Since the C family of languages is occasionally well designed they all provide a notation for octal and hexadecimal constants An octal constant must start with a zero and have only octal digits: –031, 077123 A hexadecimal constant starts with a zero and then an x: –0x012cf8

11 Copyright © 1997-2009 Curt Hill Purpose Bool values may contain one of two values: –true or false Boolean variables take one byte each Each byte consists of 8 bits, each with a possible value of 0 or 1 Bitwise operators allow us to use these bits like small arrays of bools

12 Copyright © 1997-2009 Curt Hill Packing An integer is normally 4 bytes or 32 bits An array of 4 bools occupies 4 bytes In same space 4 booleans or 32 bits? Allocation is always by bytes so we cannot allocate 25 bits

13 Copyright © 1997-2009 Curt Hill Boolean operations AND OR NOT XOR

14 Copyright © 1997-2009 Curt Hill What is needed? Declare these clusters of booleans Initialize Apply the operators Obtain the results

15 Copyright © 1997-2009 Curt Hill Declaration Any simple type may be used The preference is for integral types char gives 8 bits short gives 16 int gives 32 double gives 64

16 Copyright © 1997-2009 Curt Hill Operators Bitwise operators are the same as the bool operators except one symbol instead of two –&& is boolean AND –& is bit AND –| is OR –^ is XOR

17 Copyright © 1997-2009 Curt Hill Example int a, b, c; c = a | b This does 32 individual Ors in one operation –Usually a very fast operation 10110111 ^ 00101101 gives 10011010

18 Copyright © 1997-2009 Curt Hill What about NOT? To accomplish NOT use XOR with all ones for one operand 11111111 ^ 01100111 gives 10011000 Negation example int c; … c = c ^ 0xFFFFFFFF

19 Copyright © 1997-2009 Curt Hill Accessing results Integers cannot be used as bools There is no subscripting operation for bits either How are they accessed? Using bit operations and constant integers

20 Copyright © 1997-2009 Curt Hill Setting Values To set values –Add the power of two constants –c = 64 + 8 + 4 + 1; To check the presence of a bit –AND it with constant and check for zero –c & 8 != 0 true 8 was present false 8 was not present

21 Copyright © 1997-2009 Curt Hill Shifting There are also shift operations A left shift of one bit corresponds to multiply by 2 A right shift of one bit corresponds to divide by 2 Form is int << int –Left int is one to be shifted –Right is number of bits to shift

22 Copyright © 1997-2009 Curt Hill Example Shift c = a << 2; –Same as multiply by 4 if a and c are not signed b = c >> a; –Shift the value from c to the right by a bits and put result in b

23 Copyright © 1997-2009 Curt Hill In Theory and In Practice The usual use of bits are for named constants An Event object contains an id field, which has these flags among others –MOUSE_ENTER, MOUSE_DRAG, MOUSE_EXIT, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE –ACTION_EVENT –LIST_SELECT, LIST_DESELECT

24 Copyright © 1997-2009 Curt Hill Sets Boolean algebra and set theory are orthogonal –Any set operation or proof may be converted into a boolean operation or proof or vice versa Thus an array of booleans may act like a set of small integers

25 Copyright © 1997-2009 Curt Hill Set Operations The boolean AND is equivalent to the set Intersection –A & B could be either intersection or parallel And The boolean OR is the set Union The membership function of a set is merely determining if that integer is present


Download ppt "Copyright © 1997-2009 Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus."

Similar presentations


Ads by Google