Presentation is loading. Please wait.

Presentation is loading. Please wait.

Number Systems and Bitwise Operation

Similar presentations


Presentation on theme: "Number Systems and Bitwise Operation"— Presentation transcript:

1 Number Systems and Bitwise Operation
UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

2 DKT121: Fundamental of Computer Programming
Binary, Octal, Hexadecimal, and Decimal Binary Binary numbering system has only two possible values for each digit: 0 and 1. For example, binary number decimal number UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

3 DKT121: Fundamental of Computer Programming
Decimal Numbers Decimal The digits' weight increases by powers of 10. The weighted values for each position is determined as follows: 104 103 102 101 100 10000 1000 10 1 For example, A decimal number 4261 can be thought of as follows. 4 * * * * 1 = = 4261 (decimal) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

4 Binary, Octal, Hexadecimal, and Decimal
The digits' weight increases by powers of 2. The weighted values for each position is determined as follows: 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 For example, binary 10 is decimal 2. the binary value represents the decimal value 202. 1 * * * * * * * * 1 = = 202 (decimal) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

5 Binary Two’s Complement
The left-most bit is the sign bit. If it is 1, then the number is negative. Otherwise, it is positive. Give a negative value, represent it in binary two’s complement form as follows. write the number in its absolute value. complement the binary number. plus 1. Example, represent –2 in binary two’s complement with 16 bits for short int. Binary value of 2: b Binary complement of 2: b Plus 1: Binary two’s complement representation of -2: 0b UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

6 DKT121: Fundamental of Computer Programming
Give binary two’s complement form of a negative number, find the absolute value of the negative value as follows. Complement the binary number. Plus 1. Example, find the decimal value of (0b )2 in binary two’s complement form with 16 bits. Binary two’s complement (0b )2 Binary complement (0b )2 Plus Absolute value: (0b )2 = 210 Negative value: UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

7 DKT121: Fundamental of Computer Programming
Subtraction of a value in the computer can be treated as addition of its two’s complement. For example, the subtraction of (2-2) can be performed as 2+(-2) as follows: 0b (binary representation of 2) 0b (two’s complement representation of -2) 0b (2+(-2)) UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

8 DKT121: Fundamental of Computer Programming
Example > short i, j > i = 0b 2 > j = 0b -2 > i+j UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

9 DKT121: Fundamental of Computer Programming
Octal The octal system is based on the binary system with a 3-bit boundary. The octal number system uses base 8 includes 0 through 7. The weighted values for each position is as follows: 83 82 81 80 512 64 8 1 Binary to Octal Conversion Break the binary number into 3-bit sections from the least significant bit (LSB) to the most significant bit (MSB). Convert the 3-bit binary number to its octal equivalent. For example, the binary value equals to octal value ( )8. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

10 DKT121: Fundamental of Computer Programming
Octal to Binary Conversion Convert the octal number to its 3-bit binary equivalent. Combine all the 3-bit sections. For example, the octal value equals to binary value Octal to Decimal Conversion To convert octal number to decimal number, multiply the value in each position by its octal weight and add each value together. For example, the octal value (167)8 represents decimal value 119. 1*64 + 6*8 + 7*1 = 119 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

11 DKT121: Fundamental of Computer Programming
Hexadecimal Similar to octal, the hexadecimal system is also based on the binary system but using 4-bit boundary. The hexadecimal number system uses base 16 including the digits 0 through 9 and the letters A, B, C, D, E, and F. The letters A through F represent the decimal numbers 10 through 15. For the decimal values from 0 to 15, the corresponding hexadecimal values are listed below. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 F E D C B A Decimal Hexadecimal UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

12 DKT121: Fundamental of Computer Programming
The weighted values for each position is as follows: 163 162 161 160 4096 256 16 1 The conversion between binary value and hexadecimal value is similar to octal number,but using four-bit sections. The hexadecimal value 20A represents decimal value 522. 2* * *1 = 522 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

13 DKT121: Fundamental of Computer Programming
Following table provides all the information you need to convert from one type number into any other type number for the decimal values from 0 to16. Binary Octal Decimal Hex 0000 00 1001 11 09 0001 01 1010 12 10 A 0010 02 1011 13 B 0011 03 1100 14 C 0100 04 1101 15 D 0101 05 1110 16 E 0110 06 1111 17 F 0111 07 10000 20 1000 08 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

14 DKT121: Fundamental of Computer Programming
Bitwise Operators There are six bitwise operators: Operator Name Description & bitwise AND The bit is set to 1 if the corresponding bits in the two operands are both 1. | bitwise OR The bit is set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bit is set to 1 if exactly one of the corresponding bits in the two operands is 1. << left shift Shift the bits of the first operand left by the number of bits specified by the second operand; fill from right with 0 bits. >> right shift Shift the bits of the first operand right by the number of bits specified by the second operand; filling from the left is implementation dependent. ~ One’s complement Set all 0 bits to 1, and all 1 bits to 0. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

15 DKT121: Fundamental of Computer Programming
Example: a b a & b a | b a ^ b b << 1 a >> 1 ~a UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

16 DKT121: Fundamental of Computer Programming
/* File: bitop.ch (run in Ch only) Use Ch features “%b” and 0b */ #include <stdio.h> int main() { char a = 0b ; char b = 0b ; char c; printf("a = b%8b\n", a); printf("b = b%8b\n", b); c = a & b; printf("a & b = 0b%8b\n", c); c = a | b; printf("a | b = 0b%8b\n", c); c = a ^ b; printf("a ^ b = 0b%8b\n", c); c = b << 1; printf("b << 1 = 0b%8b\n", c); c = a >> 1; printf("a >> 1 = 0b%8b\n", c); c = ~a; printf("~a = 0b%8b\n", c); return 0; } Output: a = b b = b a & b = 0b a | b = 0b a ^ b = 0b b << 1 = 0b a >> 1 = 0b ~a = 0b UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

17 DKT121: Fundamental of Computer Programming
Logic Operators There are four logic operators: 1) ! logic NOT 2) && --- logic AND 3) || inclusive OR 4) ^^ exclusive OR (available in Ch only) a b !a a && b a || b a ^^ b 1 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming


Download ppt "Number Systems and Bitwise Operation"

Similar presentations


Ads by Google