bitwise operators

35
Bitwise operators

Upload: nelle-guerra

Post on 01-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Bitwise operators. 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 si gnificant digit. Most si gnificant digit. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bitwise operators

Bitwise operators

Page 2: Bitwise operators

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). 192010 = 1x103 + 9x102 + 2x101 + 0x100

Least significant digit

Most significant digit

Page 3: Bitwise operators

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). 1100112 =1x25+1x24+0x23+0x22+1x21+1x20

Where does the word “bit” come from?

Least significant bitMost significant bit

Page 4: Bitwise operators

Representing integers

Typical integer sizes: unsigned char = 8 bits unsigned short = 16 bits unsigned int = 32 bits

1100112

• is 001100112 when stored in an unsigned char• is 00000000001100112 when stored in an unsigned short• is 000000000000000000000000001100112 when stored in

an unsigned int

• We know that 192010 is the same as 0192010 or 00000000192010.

Page 5: Bitwise operators

Boolean (logical) operators

&& is and|| is or! is not

Page 6: Bitwise operators

Boolean (logical) operators

&& is and F && F is F F && T is F T && F is F T && T is T

Page 7: Bitwise operators

Boolean (logical) operators

|| is or F || F is F F || T is T T || F is T T || T is T

Page 8: Bitwise operators

Boolean (logical) operators

! is not !F is T !T is F

Page 9: Bitwise operators

Boolean (logical) operators

^ is xor F ^ F is F F ^ T is T T ^ F is T

T ^ T is F

Page 10: Bitwise operators

Bitwise operators (on integers)

& is bitwise and| is bitwise or~ is bitwise not (1’s complement)^ is bitwise xor (exclusive or)

Let’s substitute 1 for T and 0 for F.

Page 11: Bitwise operators

Bitwise operators (on integers)

& is bitwise and

110011 (really 00110011 in an 8-bit byte)

& 001111 (really 00001111)

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

000011 (really 00000011)

Page 12: Bitwise operators

Bitwise operators (on integers)

| is bitwise or

110011 (really 00110011 in an 8-bit byte)

| 001111 (really 00001111)

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

111111 (really 00111111)

Page 13: Bitwise operators

Bitwise operators (on integers)

~ is bitwise not (1’s complement)

If x=110011 (really 00110011 in an 8-bit byte) then ~x is 11001100.

Page 14: Bitwise operators

Bitwise operators (on integers)

^ is bitwise xor

110011 (really 00110011 in an 8-bit byte)

^ 001111 (really 00001111)

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

111100 (really 00111100)

Page 15: Bitwise operators

Other bitwise operators

<< is 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 23)

Page 16: Bitwise operators

Other bitwise operators

>> is 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

Page 17: Bitwise operators

More examples

unsigned int ui=0;ui = 10 & 7;ui = 10 | 7;ui = 10 ^ 7

unsigned char uc = ~12;

Page 18: Bitwise operators

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)

Page 19: Bitwise operators

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?

Page 20: Bitwise operators

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);

Page 21: Bitwise operators

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?

Page 22: Bitwise operators

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;

Page 23: Bitwise operators

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?

Page 24: Bitwise operators

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”);else puts(“skipped”);

if ((quiz1&s25)==s25) puts(“taken”);else puts(“skipped”);

if (quiz1&s25) puts(“taken”);else puts(“skipped”);

Page 25: Bitwise operators

Bits as binary flags.

Did both students 22 and 25 take the exam?

Page 26: Bitwise operators

Bits as binary flags.

Did both students 22 and 25 take the exam?

if ((quiz1&(s22|s25)) == (s22|s25) )puts(“taken”);

elseputs(“skipped”);

if ((quiz1&s22)!=0 && (quiz1&s25)!=0) …

Page 27: Bitwise operators

Bits as binary flags.

Did everyone except for student 25 take the exam?

Page 28: Bitwise operators

Bits as binary flags.

Did everyone except for student 25 take the exam?

if ( (quiz1&(~s25)) == (~s25) ) puts(“yes”);

else puts(“no”);

Page 29: Bitwise operators

Bits as binary flags.

I thought student 25 took the exam but I was mistaken. How can I rectify my mistake?

Page 30: Bitwise operators

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;

Page 31: Bitwise operators

Bits as binary flags.

Finally, I’d like to print out a list of all of the students that took exam 1.

Page 32: Bitwise operators

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;

}

Page 33: Bitwise operators

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;}

Page 34: Bitwise operators

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.

Page 35: Bitwise operators

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;}