Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Binary Representations. Binary Binary is a numerical system that is used to represent numeric values. Binary is a numerical system.

Similar presentations


Presentation on theme: "CS 240: Data Structures Binary Representations. Binary Binary is a numerical system that is used to represent numeric values. Binary is a numerical system."— Presentation transcript:

1 CS 240: Data Structures Binary Representations

2 Binary Binary is a numerical system that is used to represent numeric values. Binary is a numerical system that is used to represent numeric values. Unlike decimal (our traditional method of numeric representation), binary has only two digits: 0 and 1 (instead of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9). Unlike decimal (our traditional method of numeric representation), binary has only two digits: 0 and 1 (instead of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

3 Counting Binary Binary 0 1 10 10 11 11 100 100 101 101 110 110 111 111 Decimal Decimal 0 1 2 3 4 5 6 7

4 Counting To translate into decimal: To translate into decimal: Take each digit: Take each digit: Multiply the rightmost digit by 1. Multiply the rightmost digit by 1. Multiply the next rightmost digit by 2. Multiply the next rightmost digit by 2. Multiply the next rightmost digit by 4. Multiply the next rightmost digit by 4. And so on. And so on.

5 Conversion Consider 111: Consider 111: The rightmost digit is 1. 1 * 1 = 1 The rightmost digit is 1. 1 * 1 = 1 The next is 1, 1 * 2 = 2 The next is 1, 1 * 2 = 2 The next is 1, 1 * 4 = 4 The next is 1, 1 * 4 = 4 The sum of these is 7. The sum of these is 7.

6 Consider 1000 1100 (we usually group binary numbers every 4 digits) Consider 1000 1100 (we usually group binary numbers every 4 digits) The sum is: 140 The sum is: 140 10001100 *128 *64 *32 *16 *8*8*8*8 *4*4*4*4 *2*2*2*2 *1*1*1*1 =128=0=0=0=8=4=0=0

7 Lets turn 555 to binary. Lets turn 555 to binary. int test = 555; int test = 555; while(test != 0) while(test != 0) { if test is odd: print 1 if test is odd: print 1 else: print 0 else: print 0 test = (int)(test / 2); test = (int)(test / 2); } This gives us the binary result – but backwards This gives us the binary result – but backwards

8 555, is odd: 555, is odd: 277, is odd: 277, is odd: 138, is even: 138, is even: 69, is odd: 69, is odd: 34, is even: 34, is even: 17, is odd: 17, is odd: 8, is even: 8, is even: 4, is even: 4, is even: 2, is even: 2, is even: 1, is odd: 1, is odd: 0 -> done 0 -> done 1 1 0 1 0 1 0 0 0 1 1101010001 – in reverse! 1101010001 – in reverse!

9 1000101011 *512 *256 *128 *64 *32 *16 *8*8*8*8 *4*4*4*4 *2*2*2*2 *1*1*1*1 5120003208021 Sum = 512 + 32 + 8 + 2 + 1 = 555


Download ppt "CS 240: Data Structures Binary Representations. Binary Binary is a numerical system that is used to represent numeric values. Binary is a numerical system."

Similar presentations


Ads by Google