1 / 17

Number Systems and Bitwise Operation

Number Systems and Bitwise Operation. 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

Download Presentation

Number Systems and Bitwise Operation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Number Systems and Bitwise Operation DKT121: Fundamental of Computer Programming

  2. 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 0 0 1 1 10 2 11 3 100 4 101 5 110 6 1100 1010 202 DKT121: Fundamental of Computer Programming

  3. Decimal Numbers • Decimal • The digits' weight increases by powers of 10. The weighted values for • each position is determined as follows: For example, A decimal number 4261 can be thought of as follows. 4 * 1000 + 2 * 100 + 6 * 10 + 1 * 1 = 4000 + 200 + 60 + 1 = 4261 (decimal) DKT121: Fundamental of Computer Programming

  4. Binary, Octal, Hexadecimal, and Decimal • Binary • The digits' weight increases by powers of 2. The weighted values for each position is • determined as follows: For example, binary 10 is decimal 2. the binary value 1100 1010 represents the decimal value 202. 1 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202 (decimal) 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: 0b0000 0000 0000 0010 • Binary complement of 2: 0b1111 1111 1111 1101 • Plus 1: +1 • Binary two’s complement representation of -2: 0b1111 1111 1111 1110 DKT121: Fundamental of Computer Programming

  6. 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 (0b1111 1111 1111 1110)2 in binary • two’s complement form with 16 bits. • Binary two’s complement (0b1111 1111 1111 1110)2 • Binary complement (0b0000 0000 0000 0001)2 • Plus 1 +1 • Absolute value: (0b0000 0000 0000 0010)2 = 210 • Negative value: -2 DKT121: Fundamental of Computer Programming

  7. 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: 0b0000 0000 0000 0010 (binary representation of 2) 0b1111 1111 11111110 (two’s complement representation of -2) 0b0000 0000 0000 0000 (2+(-2)) DKT121: Fundamental of Computer Programming

  8. Example > short i, j > i = 0b0000000000000010 2 > j = 0b1111111111111110 -2 > i+j 0 DKT121: Fundamental of Computer Programming

  9. 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: • 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 value1 010 000 111 101 110 100 011 equals to • octal value (12075643)8. DKT121: Fundamental of Computer Programming

  10. Octal to Binary Conversion • Convert the octal number to its 3-bit binary equivalent. • Combine all the 3-bit sections. • For example, the octal value45761023 equals to binary value • 100 101 111 110 001 000 010 011. • 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 DKT121: Fundamental of Computer Programming

  11. 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. Decimal Hexadecimal DKT121: Fundamental of Computer Programming

  12. The weighted values for each position is as follows: 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*256 + 0*16 + 10*1 = 522 DKT121: Fundamental of Computer Programming

  13. 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. DKT121: Fundamental of Computer Programming

  14. Bitwise Operators • There are six bitwise operators: DKT121: Fundamental of Computer Programming

  15. Example: DKT121: Fundamental of Computer Programming

  16. /* File: bitop.ch (run in Ch only) Use Ch features “%b” and 0b */ #include <stdio.h> int main() { char a = 0b10110100; char b = 0b11011001; char c; printf("a = 0b%8b\n", a); printf("b = 0b%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 = 0b10110100 b = 0b11011001 a & b = 0b10010000 a | b = 0b11111101 a ^ b = 0b01101101 b << 1 = 0b10110010 a >> 1 = 0b11011010 ~a = 0b01001011 DKT121: Fundamental of Computer Programming

  17. Logic Operators • There are four logic operators: • 1) ! --- logic NOT • 2) &&--- logic AND • 3) || --- inclusive OR • 4) ^^ --- exclusive OR (available in Ch only) DKT121: Fundamental of Computer Programming

More Related