CSE 1320: INTERMEDIATE PROGRAMMING BITWISE...

17
4/16/2015 1 CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORS Mingon Kang The University of Texas at Arlington Revisiting printf() function (refer to revisit_printf.c) %d for Decimal (Base 10) %x, %X for hexadecimal (Base 16) %o for octal (Base 8) Do we have a binary (Base 2) format specifier? Revisiting printf() function Lecture 19: Bitwise Operators 2

Transcript of CSE 1320: INTERMEDIATE PROGRAMMING BITWISE...

Page 1: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

1

CSE 1320: INTERMEDIATE

PROGRAMMING

BITWISE OPERATORS

Mingon Kang

The University of Texas at Arlington

Revisiting printf() function (refer to revisit_printf.c)

%d for Decimal (Base 10)

%x, %X for hexadecimal (Base 16)

%o for octal (Base 8)

Do we have a binary (Base 2) format specifier?

Revisiting printf() function

Lecture 19: Bitwise Operators 2

Page 2: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

2

N-bit integer system

Most significant bit (left-

most bit) denotes the sign

bit

0 indicates positive

1 indicates negative

2’s Complement for signed representation

Lecture 19: Bitwise Operators 3

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

N-bit integer system

Most significant bit (left-

most bit) denotes the sign

bit

0 indicates positive

1 indicates negative

2’s Complement for signed representation

Lecture 19: Bitwise Operators 4

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

2′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 = −𝑥 Example:

2′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 5 = −5

2′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 −5 = 5

Page 3: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

3

N-bit integer system

2’s complement is an

operation of changing

the sign of the given

integer.

2’s Complement for signed representation

Lecture 19: Bitwise Operators 5

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

2′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 = 1′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 + 1

Then what is 1’s Complement of x?

N-bit integer system

1’s Complement of x:

The number produced after

you subtract each bits of x

from 1

1′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 0112 =1′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 3 = 1002,

where N=3

1′𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 11002 = 00112,

where N=4

2’s Complement for signed representation

Lecture 19: Bitwise Operators 6

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

2′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 = 1′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 + 1

Page 4: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

4

N-bit integer system

2’s Complement of x:

2′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 0112 == 2′ 𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 3

= 1′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡(0112) + 1

= 1002 + 1 = 1012

2’s Complement for signed representation

Lecture 19: Bitwise Operators 7

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

2′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 = 1′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 + 1

N-bit integer system

2’s Complement of x = -x

One exception to this negativity:

2′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 1002 = 2′ 𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 −4

= 1′𝑠𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡(1002) + 1

= 0112 + 1 = 1002 = −𝟒

2’s Complement for signed representation

Lecture 19: Bitwise Operators 8

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

2′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 = 1′𝑠 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 𝑥 + 1

So, this negative number is the

minimum in the range of number using N-bits

Page 5: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

5

N-bit integer system

The range of representable

integers:

When N=3:

-4 to 3

When N=4:

-8 to 7

When N:

(−2𝑁−1) 𝑡𝑜 (2𝑁−1 − 1)

2’s Complement for signed representation

Lecture 19: Bitwise Operators 9

Binary Decimal

(unsigned)

Decimal

(signed)

000 0 0

001 1 1

010 2 2

011 3 3

100 4 -4

101 5 -3

110 6 -2

111 7 -1

N-bit integer system

Now you can replace any subtraction, division,

multiplication operation using only the addition

operation !! (A Giant Step towards circuit minimization)

2’s Complement for signed representation

Lecture 19: Bitwise Operators 10

5 – 2 = ?

= 5 + (-2)

(Let, N=4 bits)

2’sComplement(2)= -2 = 1’sComplement(2)+1

= 1’sComplement(00102)+1

= 11012 + 1 = 11102

Page 6: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

6

N-bit integer system

Now you can replace any subtraction, division,

multiplication operation using only the addition

operation !! (A Giant Step towards circuit minimization)

2’s Complement for signed representation

Lecture 19: Bitwise Operators 11

5 – 2 = ?

= 5 + (-2)

= 01012 + 11102 = ? (N=4bits) 01012 11102

------

1 00112 = 3

What is Bitwise Operator?

Allows you to get, set, shift bits in an integer type

variable declared in C

Tools (operators) to be used:

Bitwise NOT (~) : Is the 1’s complement operator

Bitwise AND (&):

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

Lecture 19: Bitwise Operators 12

Page 7: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

7

What is Bitwise Operator?

Tools (operators) to be used:

Bitwise NOT (~) : Is the 1’s complement operator

Bitwise AND (&)

Bitwise OR ( | ):

0 | 0 = 0

0 | 1 = 1

1 | 0 = 1

1 | 1 = 1

Lecture 19: Bitwise Operators 13

What is Bitwise Operator?

Tools (operators) to be used:

Bitwise NOT (~) : Is the 1’s complement operator

Bitwise AND (&)

Bitwise OR ( | )

Bitwise XOR (^):

0 ^ 0 = 0

0 ^ 1 = 1

1 ^ 0 = 1

1 ^ 1 = 0

Lecture 19: Bitwise Operators 14

Page 8: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

8

What is Bitwise Operator?

Tools (operators) to be used:

Bitwise NOT (~) : Is the 1’s complement operator

Bitwise AND (&)

Bitwise OR ( | )

Bitwise XOR (^)

Left Shift Operator (<<):

x = 010011012

x << 1

Value of x = 1001101𝟎2

x << 1

Value of x = 001101𝟎𝟎2

15

• 𝑦 = 101101002

• y << 3

• Value of y = 10100𝟎𝟎𝟎2

What is Bitwise Operator?

Tools (operators) to be used:

Bitwise NOT (~) : Is the 1’s complement operator

Bitwise AND (&)

Bitwise OR ( | )

Bitwise XOR (^)

Left Shift Operator (<<)

Right Shift Operator (>>):

x = 010011012

x >> 1

Value of x = 𝟎01001102

x >> 1

Value of x =𝟎𝟎0100112

16

• 𝑦 = 101101002

• y >> 3

• Value of y = 𝟏𝟏𝟏101102

(sign extension if y is a signed

integer, although it is

architecture dependent)

Page 9: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

9

What is Bitwise Operator?

Tools (operators) to be used:

[refer to bitwise_operators_example1.c]

Bitwise NOT (~)

Bitwise AND (&)

Bitwise OR ( | )

Bitwise XOR (^)

Left Shift Operator (<<)

Right Shift Operator (>>)

17

Why bother learning it?

Packing of (Boolean) Variables

An N-bit integer variable can pack N Boolean

variables in it

How can you access 3rd from the right variable from the

packed variable?

How can you change of value to the 5th from the right

variable in the packed variable?

Lecture 19: Bitwise Operators 18

Page 10: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

10

Accessing 3rd bit from the right

Lecture 19: Bitwise Operators 19

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

? 1 & 𝑥2 = 𝑥2

0 & 𝑥2 = 0

Accessing 3rd bit from the right

20

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

0 0 0 0 0 𝟏 0 0

& & & & & & & &

Result= 0 0 0 0 0 𝑥2 0 0

𝑅𝑒𝑠𝑢𝑙𝑡 = 1 𝑖𝑓 𝑥2 = 10 𝑖𝑓 𝑥2 = 0

0 0 0 0 0 0 0 1 mask = << 2

mask =

if ( (x & (1<<2) ) != 0) printf(“1”);

else printf(“0”);

Page 11: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

11

Set 5th bit from the Right to 1

Lecture 19: Bitwise Operators 21

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

1 | 𝑥4 = 1

0 | 𝑥4 = 𝑥4

Set 5th bit from the Right to 1

Lecture 19: Bitwise Operators 22

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

0 0 0 1 0 0 0 0

| | | | | | | |

Result= 𝑥7 𝑥6 𝑥5 𝟏 𝑥3 𝑥2 𝑥1 𝑥0

0 0 0 0 0 0 0 1 mask = << 4

mask =

x = x | (1 << 4) ;

Page 12: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

12

Set 5th bit from the Right to 0

Lecture 19: Bitwise Operators 23

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

1 1 1 0 1 1 1 1

& & & & & & & &

𝑥7 𝑥6 𝑥5 𝟎 𝑥3 𝑥2 𝑥1 𝑥0

0 0 0 0 0 0 0 1 mask = << 4

~ mask =

0 0 0 1 0 0 0 0

x = x & (~(1 << 4) );

Toggle (Complement) 5th bit from the right

Lecture 19: Bitwise Operators 24

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

1 ^ 𝑥4 = 𝑥4

0 ^ 𝑥4 = 𝑥4

Page 13: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

13

Toggle (Complement) 5th bit from the right

Lecture 19: Bitwise Operators 25

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

0 0 0 1 0 0 0 0 mask =

^ ^ ^ ^ ^ ^ ^ ^

𝑥7 𝑥6 𝑥5 𝒙𝟒 𝑥3 𝑥2 𝑥1 𝑥0 R𝐞𝐬𝐮𝐥𝐭 =

x = x ^ (1 << 4);

x = x ^ 0x10;

Alternatively,

What is the integer equivalent value represented

by the bits 𝒙𝟓𝒙𝟒 𝒙𝟑?

Lecture 19: Bitwise Operators 26

𝑥7 𝑥6 𝑥5 𝑥4 𝑥3 𝑥2 𝑥1 𝑥0

Suppose, N = 8 bit integer system (unsigned)

𝒙 =

0 0 𝟏 𝟏 𝟏 0 0 0 mask =

& & & & & & & &

0 0 𝒙𝟓 𝒙𝟒 𝒙𝟑 0 0 0 𝑾𝒆 𝒈𝒆𝒕 =

We right shift this to align the three bits to the right,

so that 𝒙𝟑 becomes the least-significant bit.

0 0 𝟎 𝟎 𝟎 𝒙𝟓 𝒙𝟒 𝒙𝟑

>> 3

printf(“%d”, (x & 0x38) >> 3 );

Page 14: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

14

Multiplying x by 2𝑘

𝑥 ≪ 𝑘

Lecture 19: Bitwise Operators 27

Divide x by 2𝑘

𝑥 ≫ 𝑘

Lecture 19: Bitwise Operators 28

Page 15: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

15

Check if x is an integer power of 2

Lecture 19: Bitwise Operators 29

if( (x & (x-1)) == 0 )

printf(“YES!!”);

else

printf(“NO”);

x = 4

00100

00011

-------

00000 == 0 (YES)

x = 8

01000

00111

--------

00000 == 0 (YES)

x = 12

01100

01011

-------

01000 != 0 (No)

Even or ODD?

Lecture 19: Bitwise Operators 30

if( x & 1 == 1 )

printf(“ODD!!”);

else

printf(“EVEN”);

Page 16: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

16

Multi-options as a single parameter

In Windows Programming,

Multiple options can

be passed as a single

parameter by using

bitwise OR operator

Lecture 19: Bitwise Operators 31

HWND WINAPI CreateWindowEx(

_In_ DWORD dwExStyle,

_In_opt_ LPCTSTR lpClassName,

_In_opt_ LPCTSTR lpWindowName,

_In_ DWORD dwStyle,

_In_ int x,

_In_ int y,

_In_ int nWidth,

_In_ int nHeight,

_In_opt_ HWND hWndParent,

_In_opt_ HMENU hMenu,

_In_opt_ HINSTANCE hInstance,

_In_opt_ LPVOID lpParam

);

Multi-options as a single parameter

WS_EX_APPWINDOW| WS_EX_CLIENTEDGE |

WS_EX_RIGHTSCROLLBAR| ..

https://msdn.microsoft.com/en-

us/library/windows/desktop/ms632680(v=vs.85).

aspx

Lecture 19: Bitwise Operators 32

Page 17: CSE 1320: INTERMEDIATE PROGRAMMING BITWISE OPERATORSbiomecis.uta.edu/~mkang/teaching/cse1320_15s/18.Bitwise... · 2015. 4. 16. · 4/16/2015 2 N-bit integer system Most significant

4/16/2015

17

References

Bitwise Operators in C

(http://en.wikipedia.org/wiki/Bitwise_operations_i

n_C )

Lecture 19: Bitwise Operators 33