Download presentation
Presentation is loading. Please wait.
Published byMargery Preston Modified over 9 years ago
1
Bitwise operators
2
Representing integers We typically think in terms of decimal (base 10) numbers. Why? A decimal (or base 10) number consists of a sequence of decimal digits (0,1,…,9). 1920 10 = 1x10 3 + 9x10 2 + 2x10 1 + 0x10 0 Least significant digit Most significant digit
3
Representing integers All numbers within the computer are stored as binary numbers. Why? A binary (or base 2) number consists of a sequence of bits (0,1). 110011 2 =1x2 5 +1x2 4 +0x2 3 +0x2 2 +1x2 1 +1x2 0 Where does the word “bit” come from? Least significant bit Most significant bit
4
Representing integers Typical integer sizes: unsigned charis 8 bits unsigned shortis 16 bits unsigned intis 32 bits 110011 2 is 00110011 2 when stored in an unsigned char is 0000000000110011 2 when stored in an unsigned short is 00000000000000000000000000110011 2 when stored in an unsigned int We know that 1920 10 is the same as 01920 10 or 000000001920 10.
5
BOOLEAN (LOGICAL) OPERATORS
6
Boolean (logical) operators &&and ||or !not ^xor (exclusive or)
7
Boolean (logical) operators &&and F && F is F F && T is F T && F is F T && T is T
8
Boolean (logical) operators ||or F || F is F F || T is T T || F is T T || T is T
9
Boolean (logical) operators !not !F is T !T is F
10
Boolean (logical) operators ^xor F ^ F is F F ^ T is T T ^ F is T T ^ T is F
11
BITWISE OPERATORS (ON INTEGERS)
12
Bitwise operators (on integers) &bitwise and |bitwise or ~bitwise not (1’s complement) ^bitwise xor (exclusive or) -negation (2’s complement) Let’s substitute 1 for T and 0 for F.
13
Bitwise operators (on integers) &bitwise and 110011(really 00110011 in an 8-bit byte) &001111(really 00001111) ------------ 000011(really 00000011)
14
Bitwise operators (on integers) |bitwise or 110011(really 00110011 in an 8-bit byte) |001111(really 00001111) ------------ 111111(really 00111111)
15
Bitwise operators (on integers) ~bitwise not (1’s complement) If x is 110011 (really 00110011 in an 8-bit byte) then ~x is 11001100.
16
Bitwise operators (on integers) -negation (2’s complement) algorithm: 1.Perform 1’s complement (~). 2.Add 1. If x is 110011 (really 00110011 in an 8-bit byte) then ~x is11001100 -x is11001101(after add 1 to previous)
17
Bitwise operators (on integers) ^bitwise xor 110011(really 00110011 in an 8-bit byte) ^001111(really 00001111) ------------ 111100(really 00111100)
18
Other bitwise operators <<shift bits to the left 1 shift to the left is the same as multiplying by 2. Examples 10 << 1 is 20 7 << 1 is 14 7 << 3 is 56(same as multiplying by 2 3 )
19
Other bitwise operators >>shift bits to the right 1 shift to the right is the same as integer division by 2. Examples 10 >> 1 is 5 27 >> 3 is 3
20
More examples unsigned int ui=0; ui = 10 & 7; ui = 10 | 7; ui = 10 ^ 7 unsigned char uc = ~12;
21
Bits as binary flags. An int is 32 bits so we can number each student in the class from 0..31. If the bit for a particular student is 1, then that indicates that they took a quiz. First, let’s define the students. #define S0 (1<<0) #define S1 (1<<1) #define S2 (1<<2) #define S3 (1<<3) #define S4 (1<<4) . #define S31 (1<<31)
22
Bits as binary flags. Now let’s define a quiz. unsigned int quiz1 = 0; How can we indicate that students 0, 5, and 9 took quiz 1?
23
Bits as binary flags. Now let’s define a quiz. unsigned int quiz1 = 0; How can we indicate that students 0, 5, and 9 took quiz 1? quiz1 = (s0 | s5 | s9);
24
Bits as binary flags. Now here comes student 12. He takes the quiz on a subsequent day because he was ill. How do we update quiz1 to indicate that student 12 also took the quiz?
25
Bits as binary flags. Now here comes student 12. He takes the quiz on a subsequent day because he was ill. How do we update quiz1 to indicate that student 12 also took the quiz? quiz1 |= s12;
26
Bits as binary flags. I’d like to write a message that indicates whether or not student 25 took the exam? How can I do that?
27
Bits as binary flags. I’d like to write a message that indicates whether or not student 25 took the exam? How can I do that? if ((quiz1&s25)!=0)puts(“taken”); elseputs(“skipped”); if ((quiz1&s25)==s25)puts(“taken”); elseputs(“skipped”); if (quiz1&s25)puts(“taken”); elseputs(“skipped”);
28
Bits as binary flags. Did both students 22 and 25 take the exam?
29
Bits as binary flags. Did both students 22 and 25 take the exam? if ((quiz1&(s22|s25)) == (s22|s25) ) puts(“taken”); else puts(“skipped”); if ((quiz1&s22)!=0 && (quiz1&s25)!=0) …
30
Bits as binary flags. Did everyone except for student 25 take the exam?
31
Bits as binary flags. Did everyone except for student 25 take the exam? if ( (quiz1&(~s25)) == (~s25) )puts(“yes”); elseputs(“no”);
32
Bits as binary flags. I thought student 25 took the exam but I was mistaken. How can I rectify my mistake?
33
Bits as binary flags. I thought student 25 took the exam but I was mistaken. How can I rectify my mistake? quiz1 = quiz1 & (~s25); quiz1 &= ~s25;
34
Bits as binary flags. Finally, I’d like to print out a list of all of the students that took exam 1.
35
Bits as binary flags. Finally, I’d like to print out a list of all of the students that took exam 1. int which = 1; for (int i=0; i<32; i++) { ? which <<= 1; }
36
Bits as binary flags. I’d like to print out a list of all of the students that took exam 1. int which = 1; for (int i=0; i<32; i++) { if (quiz1&which) printf( “student %d took the quiz. \n”, i ); which <<= 1; }
37
Bits as binary flags. Say I also have quiz1 and quiz2. I’d like a list of all of the students that took quiz1 or quiz2 but not both.
38
Bits as binary flags. Say I also have quiz1 and quiz2. I’d like a list of all of the students that took either quiz1 or quiz2 but not both. int which = 1; for (int i=0; i<32; i++) { if ( (quiz1&which) ^ (quiz2&which) ) printf( “student %d took either but not both. \n”, i ); which <<= 1; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.