Understanding Number Systems: Decimal, Octal, Hexadecimal, Binary

cs 240 lecture 3 n.w
1 / 20
Embed
Share

Explore the Decimal, Octal, Hexadecimal, and Binary number systems, their representations, and practical applications. Discover how different bases can be utilized in programming for memory management, bitwise operations, and more.

  • Number Systems
  • Decimal
  • Octal
  • Hexadecimal
  • Binary

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS 240 Lecture 3 Bases and Representations, Memory, Pointers, Arrays, For-Loops

  2. Number Systems Decimal The Decimal system is the one we've grown up with, where every number is a sequence of digits of which there are only 10 values. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Any number can be decomposed as a weighted sum of powers of 10. 2(10)3+ 0(10)2+ 1(10)1+ 8(10)0= 2018 This is a unique representation when only digits 0 through 9 are allowed. We can switch to another number system simply by changing the digits and the base we're working with. All of the arithmetic we already know for the decimal system will apply to the new bases as well.

  3. Number Systems Octal Octal is the base 8 number system. 0, 1, 2, 3, 4, 5, 6, 7 1(8)2+ 2(8)1+ 3(8)0= 1238= 83 For systems other than base 10, a subscript next to the number indicates the base of the number system. The benefits of the octal number system come from it's closeness with the binary number system as seen below. 0002= 08, 0012= 18, 0102= 28, 0112= 38, 1002= 48, 1012= 58, 1102= 68, 1112= 78. To provide an int value in octal in C, precede the digits with 0. Example: 0123

  4. Number Systems Hexadecimal Octal is the base 16 number system. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f 7(16)1+ f(16)0= 7f16= 247 For systems with a base greater than 10, the letters of the alphabet are used for higher digits. In arithmetic using the letters, treat those digits as their position in the ordering. a = 10, b = 11, c = 12, d = 13, e = 14, f = 15 This system is generally preferred over octal as it takes exactly 2 hex digits to represent a single byte of data. To provide an int value in hex in C, precede the digits with 0x. Example: 0x12ab007f

  5. Number Systems Binary Binary is the base 2 number system. 0, 1 1(2)7+ 0(2)6+ 1(2)5+ 0(2)4+ 1(2)3+ 1(2)2+ 0(2)1+ 0(2)0= 101011002= 172 It's often better to operate in other number systems for arithmetic purposes. The binary number system lends itself better to logical operations on numbers which we will discuss later on in the course. To provide an int value in binary in C, precede the digits with 0b. Example: 0b00110011

  6. Number Systems Intrinsic Value It's important to note that base number systems are just representations of the intrinsic value of a number. 1510= f16 They are the same number! When considering which representation to use, there is no operational different. You decide on a representation based on practicality of interpretation. Hex is a good representation for memory addresses and arbitrary byte data Octal is a great for logical bit groups of three, like file permissions. Binary is useful for bit fields and in-depth bit manipulation required by some applications.

  7. Memory An Abstract View void main() { int a = 2; int b = 5; int a int b 2 5 Here, we simply consider variables to be labeled boxes which contain values. At a certain point in the execution, a variable that has been declared will have a value. Fails to take into account numeric addresses of memory locations. ... } Useful for solving problems in your head or on paper.

  8. Memory A Realistic View void main() { char a = 0x00; int b = 0x12345678; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 0x78 b 0x82ff6f82 0x56 0x82ff6f83 0x34 Memory is organized byte- by-byte in a long line. Variables are associated with one address, even if they span more than one. 0x82ff6f84 0x12 0x82ff6f85 *junk* 0x82ff6f86 *junk*

  9. Memory A Realistic View void main() { char a = 0x00; int b = 0x12345678; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 0x78 b 0x82ff6f82 0x56 0x82ff6f83 0x34 Also, note that the bytes of b are in reverse order. This is called Little Endian and is characteristic of Intel x86 and AMD64 Processors. 0x82ff6f84 0x12 0x82ff6f85 *junk* 0x82ff6f86 *junk*

  10. Memory A Realistic View void main() { char a = 0x00; int b; Address Value Variable 0x82ff6f80 0x00 a 0x82ff6f81 *junk* b 0x82ff6f82 *junk* Memory locations are ever- present and are reused constantly. Newly-declared variables in a function will contain junk data until assigned a value. 0x82ff6f83 *junk* 0x82ff6f84 *junk* 0x82ff6f85 *junk* 0x82ff6f86 *junk*

  11. Memory Addresses as Values Often times, it's useful to use the address of a variable programmatically. This is something that isn't easy or even possible in many other programming languages. Computer hardware generally communicate with programs through addresses, indicating where data should be written to memory and where it should be read from. This bypasses the use of variable names entirely. In theory, you could write an entire program without variables and still have it make use of memory.

  12. Memory Addresses as Values Pointers Pointers are a type of variable which are specifically designed to hold memory addresses. Addresses on 32-bit systems are 4 bytes, while on 64-bit systems they're 8 bytes! Declaring a pointer variable requires three things: a type, an asterisk (*), and a name; type * name; You can consider it to be a usual declaration statement by taking the type and asterisk together to be its own type. int * iaddr; int* iaddr; The asterisk can be to either side of the space, but convention favors the right. char *cptr;

  13. Pointers Accessing Variable Addresses To get the address of a variable in C, we use the & operator. int sum; int *iaddr; iaddr = ∑ The & operator can be read as "address of". So, the above example the value to the right would be the "address of" sum. The expression will produce an address of the specific type corresponding to the variable. int variables give int* type addresses, char variables give char*, etc.

  14. Pointers Accessing Values at Addresses To get the value at an address in C, we use the * operator. int sum; int *iaddr = 0x82ff6f81; sum = *iaddr; Don't get confused! The *iaddr in line 2 has different meaning from the *iaddr in line 3. The * operator can be read as "value at" The "value at" iaddr. The type of the pointer indicates what type of value to return. char* gives 1 byte starting from the address. int* gives 4 bytes starting from the address.

  15. Arrays One variable, Many values Arrays are a primitive collection structure in C which houses multiple variables under one name. Each sub-variable is called an element of the array. An array is declared like a variable, and so it's declaration has three specific parts: a type, a name, and a number of elements. type name[number]; In the above example, line is an array of 1000 characters. In the C99 standard, the number of elements is allowed to be variable. In older standards, the number of elements must be constant or a literal value. char line[1000];

  16. Arrays In Memory char buffer[6]; buffer[0] = 'b'; buffer[1] = 'u'; Address Value Variable 0x82ff6f80 'b' buffer[0] 0x82ff6f81 'u' buffer[1] 0x82ff6f82 'f' buffer[2] Accessing elements of the array is done with the bracket operator ([]). The index of the element you want accessed goes inside. 0x82ff6f83 'f' buffer[3] 0x82ff6f84 'e' buffer[4] 0x82ff6f85 'r' buffer[5] 0x82ff6f86 *junk*

  17. Arrays Essentially a Pointer Array variables are essentially a pointer variable with one difference: the memory allocation of elements. Pointers allocate space for an address. Arrays allocate space for elements. Array variables when used without the [] operator will instead be treated as the address of the first element of the array. Pointer variables can be used with the [] operator to treat an address as the 0th element of an array. char *chaddr = 0x1248feed; The above example modifies the address that is 4 character-widths away from 0x1234feed (which is 0x1234fef1). chadder[4] = '\n';

  18. [] operator on Pointers Same as Array! char *cptr; cptr = 0x82ff6f80; cptr[1] = 'c'; Address Value Variable 0x82ff6f80 *junk* 0x82ff6f81 'c' 0x82ff6f82 *junk* This allows us to reference other locations in memory based on addresses without variable names. Note that attempting to access memory that your program did not allocate is likely to result in error. 0x82ff6f83 *junk* 0x82ff6f84 *junk* 0x82ff6f85 *junk* 0x82ff6f86 *junk*

  19. Control Flow for-statement Going back to a topic we skipped over, now is a good time to introduce the for-statement. Like while-statements, for-statements are looping control flow compound statements used for repeated execution of statements. However, they include additional parts which make them a more powerful looping statement. for (init; cond; inc) Within the for-statement's parentheses, there are three parts: the initialization, the condition, and the "increment". The "increment" doesn't always increment. for(i=0; i<4; i++)

  20. Control Flow for-statement for (i = 0; i < N; i++) { doSomething(); } The for-statement isn't really all that different. Above are two equivalent snippets of code. Any time you can use a for-loop, you can use a while-loop instead. i = 0; while (i < N) { doSomething(); i++; } Exercise: Write an equivalent for-loop for the while-loop on Slide 17 of lecture 2.

Related


More Related Content