/
Bitwise Operators Andy Wang Bitwise Operators Andy Wang

Bitwise Operators Andy Wang - PowerPoint Presentation

lois-ondreau
lois-ondreau . @lois-ondreau
Follow
343 views
Uploaded On 2019-06-22

Bitwise Operators Andy Wang - PPT Presentation

Object Oriented Programming in C COP 3330 Bits and Bytes A bit is the smallest unit of storage in a computer It store 0 or 1 A byte consists of 8 bits Smallest unit of directl y ID: 759746

int cout number1 displaybits cout int displaybits number1 bitflags amp 11101011 00011010 mask unsigned examples bits num bitwise cpp bit 00110110 shift

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Bitwise Operators Andy Wang" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Bitwise Operators

Andy Wang

Object Oriented Programming in C++

COP 3330

Slide2

Bits and Bytes

A

bit

is the smallest unit of storage in a computer

It store 0 or 1

A

byte

consists of 8 bits

Smallest unit of directl

y

addressable storage

The smallest built-in data type is the

char

1 byte on most systems

What if we want to access individual bits?

Must use the

bitwise operators

Caution: bitwise operators may be machine dependent

Most machines use 2’s complement format

Slide3

Base Conversion

15

10

-> 1111

2

15 % 2 = 1 // least significant bit

15 / 2 = 7

7 % 2 = 1 // next bit

7 / 2 = 3

3 % 2 = 1 // next bit

3 / 2 = 1

1 % 2 = 1 // next bit

1 / 2 = 0 // terminate

Slide4

Base Conversion

1111

2

15

10

1x2

3

+ 1x2

2

+ 1x2

1

+ 1x2

0

= 8 + 4 + 2 + 1 = 15

Slide5

Two’s Complement

Suppose we have 4-bit integers

Non-negative numbers

Just convert base 10 numbers to base 2

0

10

= 0000

2

1

10

= 0001

2

2

10

= 0010

2

To add two numbers, just add the corresponding bits

1

10

+

2

10

= 0001

2

+ 0010

2

= 0011

2

= 1x2

1

+ 1x2

0

= 3

10

Slide6

Two’s Complement

Negative numbers

Flip all the bits of a non-negative numbers, then add 1

-1

10

= flip(0001

2

) + 1 =

1110

2

+ 1 = 1111

2

0

10

= flip(0000

2

) + 1 = 1111

2

+ 1 = 0000

2

If the uppermost bit is one, the number is negative

To add a non-negative number with a negative number, just add

1

10

+ (-1)

10

= 0001

2

+

1111

2

=

0000

2

=

0

Slide7

Two’s Complement

To add two negative numbers, just add as well

(-1)

10

+ (-1)

10

=

1111

2

+

1111

2

=

1110

2

To decode a negative number, subtract the number by 1, and flip all the bits

1110

2

= negative flip(1110

2

– 1) = negative flip(1101

2

) = negative 0010

2

= negative (1x2

1

) = negative 2

10

Slide8

The Bitwise Operators

Operator

Name

Arity

Description

&

Bitwise AND

Binary

Similar to the

&&

operator,

but on a bit-by-bit basis.

|

Bitwise OR

Binary

Similar

to the || operator, but on a bit-by-bit basis.

^

Bitwise Exclusive OR

Binary

Set to 1 if one of the corresponding bits is

1, or set to 0 otherwise.

~

Complement

Unary

Flips the bits in the operand.

Slide9

The Bitwise Operators

Operator

Name

Arity

Description

<<

Left shift

Binary

Shifts the bits of the first operand to the left by the number of bits specified in the second operand.

Right fill with 0 bits.

>>

Right shift

Binary

Shifts

the bits of the first operand to the right by the number of bits specified in the second operand. Left fill with 0’s for positive numbers, 1’s for negatives (machine dependent).

Slide10

Shortcut Assignment Operators

x

&

= y means x = x

&

y

x

|= y means x = x | y

x

^= y means x = x ^ y

x

<<= y means x = x << y

x

>>= y means x = x >> y

Slide11

Examples

Suppose we have the following code

s

hort x = 6891;

s

hort y = 11318;

Assume short is 2 bytes (16 bits)

x: 00011010 11101011

y: 00101100 00110110

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

x & y:

Slide12

Examples

Suppose we have the following code

s

hort x = 6891;

s

hort y = 11318;

Assume short is 2 bytes (16 bits)

x: 00011010 11101011

y: 00101100 00110110

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

x & y: 00001000 00100010 (2082)

Slide13

Examples

x: 00011010 11101011

y: 00101100 00110110

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

x | y:

x

:

00011010

11101011

y:

00101100

00110110

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

x ^ y:

Slide14

Examples

x: 00011010 11101011

y: 00101100 00110110

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

x | y: 00111110 11111111 (16127)

x

:

00011010

11101011

y:

00101100

00110110

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

x ^ y:

Slide15

Examples

x: 00011010 11101011

y: 00101100 00110110

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

x | y: 00111110 11111111 (16127)

x

:

00011010

11101011

y:

00101100

00110110

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

x ^ y:

00110110 11011101 (14045)

Slide16

Examples

x: 00011010 11101011

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

x <<

2

:

y:

00101100 00110110

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

y >> 4:

x:

00011010 11101011

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

~x:

Slide17

Examples

x: 00011010 11101011

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

x <<

2

: 01101011 10101100 (27564)

y:

00101100 00110110

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

y >> 4:

x:

00011010 11101011

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

~x:

Slide18

Examples

x: 00011010 11101011

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

x <<

2

: 01101011 10101100 (27564)

y:

00101100 00110110

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

y >> 4:

00000010 11000011 (707)

x:

00011010 11101011

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

~x:

Slide19

Examples

x: 00011010 11101011

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

x <<

2

: 01101011 10101100 (27564)

y:

00101100 00110110

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

y >> 4:

00000010 11000011 (707)

x:

00011010 11101011

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

~x:

11100101 00010100 (-6892)

Slide20

Code Examples

http://www.cs.fsu.edu/~myers/cop3330/examples/bitwise/ex1.cpp

Slide21

Code Examples

#include <

iostream

>

#include <

iomanip

>

u

sing

std

::

cout

;

u

sing

std

::

endl

;

i

nt

main() {

short x = 6891, y = 11318;

cout

<< "x = " << x << "\

ny

= " << y <<

endl

;

cout

<< "x

&

y = " << (x

&

y) <<

endl

;

cout

<< "x | y = " << (x | y) <<

endl

;

cout

<< "x ^ y = " << (x ^ y) <<

endl

;

cout

<< "x << 2 = " << (x << 2) <<

endl

;

cout

<< "y >> 4 = " << (y >> 4) <<

endl

;

cout

<< "~x = " << ~x <<

endl

;

}

Slide22

Examples from Dietel

http://www.cs.fsu.edu/~myers/deitel5c++/

ch22/Fig22_06/fig22_06.cpp

Display bit values

http://www.cs.fsu.edu/~myers/deitel5c++/

ch22/Fig22_08/fig22_08.cpp

&

, |, ^, and ~ bitwise operators

http://www.cs.fsu.edu/~myers/deitel5c++/

ch22/Fig22_11/fig22_11.cpp

<< and >> bitwise operators

Slide23

fig22_06.cpp

#include <

iostream

>

#include <

iomanip

>

u

sing namespace

std

;

v

oid

displayBits

(unsigned value) {

const

int

SHIFT = 8*

sizeof

(unsigned) -1;

const

unsigned MASK = 1 << SHIFT; // 1000….

cout

<<

setw

(10) << value << “ = “;

for (unsigned j = 1; j < SHIFT + 1;

j++

) {

if (value

&

MASK)

cout

<< ‘1’;

value <<=

1;

else

cout

<< ‘0’;

if (j % 8 == 0)

cout

<< ‘ ‘;

}

cout

<<

endl

;

}

Slide24

fig22_06.cpp

i

nt

main() {

unsigned

inputValue

;

cout

<< “Enter an unsigned integer: “;

cin

>>

inputValue

;

displayBits

(

inputValue

);

return 0;

}

Slide25

fig22_08.cpp

#include <

iostream

>

#include <

iomanip

>

u

sing namespace

std

;

v

oid

displayBits

(unsigned value) {

const

int

SHIFT = 8*

sizeof

(unsigned) -1;

const

unsigned MASK = 1 << SHIFT;

cout

<<

setw

(10) << value << “ = “;

for (unsigned j = 1; j < SHIFT + 1;

j++

) {

if (value

&

MASK)

cout

<< ‘1’;

value <<= 1;

else

cout

<< ‘0’;

if (j % 8 == 0)

cout

<< ‘ ‘;

}

cout

<<

endl

;

}

Slide26

fig22_08.cpp

i

nt

main() {

unsigned number1, number2, mask,

setBits

;

number1 = 179876355; mask = 1;

cout

<< “The result of combining the following\n”;

displayBits

(number1);

displayBits

(mask);

cout

<< “using the bitwise AND operator

&

is\n”;

displayBits

(number1

&

mask);

number1 = 15;

setBits

= 241;

cout

<< “The result of combining the following\n”;

displayBits

(number1);

displayBits

(

setBits

);

cout

<< “using the bitwise

OR

operator

|

is\n”;

displayBits

(number1

|

setBits

);

Slide27

fig22_08.cpp

number1 = 139; number2 = 199;

cout

<< “The result of combining the following\n”;

displayBits

(number1);

displayBits

(number2);

cout

<< “using the bitwise exclusive OR operator

^

is\n”;

displayBits

(number1

^

number2);

number1 = 21845;

cout

<< “\

nThe

one’s complement of\n”;

displayBits

(number1);

cout

<<

“is”;

displayBits

(~number1);

return 0;

}

Slide28

fig22_11.cpp

#include <

iostream

>

#include <

iomanip

>

u

sing namespace

std

;

v

oid

displayBits

(unsigned value) {

const

int

SHIFT = 8*

sizeof

(unsigned) -1;

const

unsigned MASK = 1 << SHIFT;

cout

<<

setw

(10) << value << “ = “;

for (unsigned j = 1; j < SHIFT + 1;

j++

) {

if (value

&

MASK)

cout

<< ‘1’;

value <<= 1

;

else

cout

<< ‘0’;

if (j % 8 == 0)

cout

<< ‘ ‘;

}

cout

<<

endl

;

}

Slide29

fig22_11.cpp

i

nt

main() {

int

number1 = -2000;

cout

<< “The result of left shifting\n”;

displayBits

(number1);

cout

<< “8 bit positions using the left-shift operator is\n”;

displayBits

(number1 << 8);

cout

<< “\

nThe

result of right shifting\n”;

displayBits

(number1);

cout

<< “8 bit positions using the right-shift operator is\n”;

displayBits

(number1 >> 8);

return 0;

}

Slide30

BitFlags Examples

http://www.cs.fsu.edu/~myers/cop3330/examples/bitwise/bitflags

/

Set up to store a set of on/off flags

On a system with 4-byte integers, a

BitFlag

object can store 32 flags (using one variable for all flags)

Slide31

bitflags.h

c

lass

BitFlags

{

public:

BitFlags

();

void Set(

int

num

);

void Unset(

int

num

);

void Flip(

int

num

);

bool Query(

int

num

)

const

;

private:

int

Mask(

int

num

)

const

;

unsigned

int

flags;

const

int

numflags

;

};

Slide32

bitflags.cpp

#include <

iostream

>

#include “

bitflags.h

BitFlags

::

BitFlags

() :

numflags

(

sizeof

(

int

)*8) {

flags = 0;

}

i

nt

BitFlags

::Mask(

int

num

)

const

{

return (1 <<

num

);

}

v

oid

BitFlags

::Set(

int

num

) {

flags = flags | Mask(

num

);

}

Slide33

bitflags.cpp

void

BitFlags

::Unset(

int

num

) {

flags =

flags

&

~Mask(

num

);

}

v

oid

BitFlags

::Flip(

int

num

) {

flags = flags ^ Mask(

num

);

}

Bool

BitFlags

::Query(

int

num

) {

return (flags

&

Mask(

num

));

}

Slide34

main.cpp

#include <

iostream

>

#include “

bitflags.h

u

sing namespace

std

;

int

main() {

BitFlags

b;

for (

int

j = 0; j < 32; j += 2)

b.Set

(j);

for (

int

j = 0; j < 32;

j++

) // reverse order…

cout

<<

b.Query

(j);

cout

<<

endl

;

Slide35

main.cpp

b.Set

(5);

b.Unset

(8);

b.Flip

(31);

for (

int

j = 0; j < 32;

j++

)

cout

<<

b.Query

(j);

cout

<<

endl

;

return 0;

}