Download presentation
Presentation is loading. Please wait.
Published byRodney Gibbs Modified over 9 years ago
1
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education
2
A short divergence into memory Before we look at other topics we need to understand how memory works Things to learn in this chapter –What is computer memory? –What does it look like to a computer program? –How do we find things in memory?
3
What is Memory It’s a bunch of bits! Looks like a large linear array Find things by “indexing” into the array with an unsigned number Most computers support 1 byte (8 bit) addressing –A byte will be at addresses 0x100, 0x101 and 0x102 –A word will be at address 0x100 and 0x104 and 0x108 A “word” is 4 bytes 32 bits = 4 bytes = 1 word 8 bits = 1 byte
4
Endianess When we looked at binary numbers we read them left to right so that 1101 = 13. The left most digit is the largest. –This is called Big Endianess If we switch 13 around, we get 1011 = 13 –This is called Small Endianess
5
Endianess Big Endian – byte 0 contains most significant bits (used by Apple Mac computers) Little Endian – byte 0 contains least significant bits (used by microsoft computers) Remember, a byte contains 8 bits.
6
More words - Alignment Alignment – If a piece of data is saved on an address that is a multiple of its size Example, a 32 bit integer –1010 1010 1010 1111 1010 1010 1010 1111 –This will be stored as 4 bytes in memory (8 bits in a byte) –This means it is stored in 4 separate spots in memory Byte 1 Byte 2Byte 3Byte 4
7
Alignment If those 4 bytes are stored in memory next to each other, then the memory is aligned. Imagine if two bytes were stored next to each other, but two were somewhere else. That is unaligned memory Byte 3stuff Byte 2 stuff Byte 1 Address 0x001 0x002 stuff UNALIGNED Byte 4
8
Memory Partitions The memory that a program uses will be split into different parts
9
Memory Layout Example x result Global variable Local variable
10
Pointers Pointers are variables that are used to save memory addresses. They don’t save the real data inside of them. Pointers just save the address of where the real data is stored We often use pointers in programming Examples: C++ –object * newobject = new object(); –string * str = new string(); Java –String a = new String(“Hello”); –A pointer works like normal variables except that the program will not allocate memory for that variable at the start of the program. –Only when the program gets to that line of code will memory be used
11
Pointers When you make variables in programming, they can either be static or dynamic –Static string str; int x; –Dynamic string * str = new string(); int * x = new int(); –In Java, almost all variables are Dynamic. The exception is if you use a “native type” like int.
12
Pointers Static variables take up memory when the program starts up. The program has already saved memory for that variable. The memory the program sees when it starts already has space taken up by static variables. This slows down the time it takes to start a program That space will now be reserved for that static variable for the entire time the program runs Dynamic variables are only created when the program gets to that line in code. Then the program will take the amount of memory it needs to use
13
Pointers Advantages of dynamic variables –Programs do not take up as much memory at start time, which means they load faster –More ability to manage memory by the programmer Disadvantages –If the programmer forgets to delete this memory, the code will “bloat.” –“Code bloat” mean the program will get slower and slower because the program uses more and more memory
14
Pointers What is a pointer? –A pointer is a variable that saves an address in memory –It "points" to a place in memory where the real data is stored –int * x; - x is a variable and in memory it contains a 32-bit address. –int x; - x is a variable and in memory it contains an integer
15
Pointers What pointers look like int x = 5; int * y = &x; cout << x << “ “ << y << endl; What is the result? –5 0x4FA09E05 The integer x displays the integer, the pointer y is the address of x. The “&” symbol will return the address of an object. The pointer "y" will hold that address
16
Pointers In conclusion –A pointer is a variable that contains the address of data –The address of operator is “&” and the pointer symbol is “*”
17
Vectors and Arrays Vectors are used to protect programmers from hurting themselves. –They resize automatically and check for to make sure you don't put too many things into a vector Behind vector is an array. Arrays are hard to work with though, and vectors make things easier When we talk about memory we will use the word array. An array is an indexed list.
18
Arrays In C++, you can get dynamic memory from the heap with a new operator and a pointer new [] returns a block of memory from the heap. How much memory? That depends on the data type of the pointer To delete the memory when you are done with it, and return it to the heap so that other variables can use that memory, use delete [] a;
19
Address Calculation x is a pointer. What is x+33 ? It’s still a pointer, but it points to a different place in memory now. The distance that it goes in memory depends on the type of data being pointed to. A int will point to 32 bits of data, but a double points to 64
20
Pointer Arithmetic
21
Pointers and Arrays
22
Java Notes Note: We can’t do this in Java (showing the addresses of actual variables) This is because Java only determines the address of variables when the program is run. This way makes it more secure. You cannot try to take over memory from other pieces of software But all Java variables and memory is done dynamically. The Java Virtual Machine will make sure that all memory is correctly used and deleted when it should be
23
Strings as arrays What are strings? –A list of characters put together in memory –The sentence: Hello World!! !\0!DLROWOLLEH 1310141211987654321 What’s the thing at the end? It’s a null terminator, which tells the computer that the string is finished. Each character is one byte of data (in ASCII)
24
Bit Manipulations You should have a general understanding about how memory and data is organized within a computer. This data is all stored in binary and in 32 bit words (4 bytes). Sometimes, a program may need to look at in individual bit of data. For example, a mouse sends one word of data to the computer that describes the x and y coordinates What part of the 32 bit word is for x and y? To find and use them, you need to be able to work with individual bits
25
Bitwise AND (&) and OR (|) The “&” symbol in programming will do a bitwise AND operation. It means it will give a logical AND operation on two numbers by their individual bits The “|” symbol in programming will do a bitwise OR operation. Meaning, it will look at an integer, one bit at a time, and apply the OR binary operation
26
Bitwise OR and AND take two bits and output the result AND will return a true if both inputs are true, false otherwise OR will return true if at least one input is true
27
Bitwise You can even take a bitwise AND or OR on a larger number Example 1001101 101011101 & 0101110 | 010110110 =
28
Mouse example A mouse will send the computer 32 bits of data –Bits 0-7 are for the x coordiniate –Bits 8-15 are for y coordinate –Bits 16-17 are for what mouse button was clicked –We can ignore bits 18-31 What if we want to get the x coordinate from the data?
29
Mouse example To get only a few bits of data from a number, we can use the AND operation This is called an AND "bit mask." It means we can block out and mask the numbers we don't want to use To get the x coordinate, use a –0x000ff bit mask (0000 0000 0000 1111 1111) –x_position = 0x1a34c & 0x000ff
30
Mouse example Can you create bit masks for the y coordinate and the button information? Notice the y coordinate is located in the middle of the number. What's needed next is a bit-wise operation that can move bits around
31
The shift operator What is this shifting? –Make numbers bigger or smaller in value by moving bits left or right –“>>” will shift bits right –“<<“ will shift bits left –( 5 << 3 ) = 0101 2 << 011 2 - means shift the one by three bits to the left –( 5 << 3) = 0101 2 << 011 2 = 0101000 2 = 40 –5 * 2^3 = 5 * 8 = 40
32
Shifting What happens if we shift the other direction? –32 >> 2 = 100000 2 >> 10 2 –Shift 32 to by 2 bits to the right –Need to move the numbers to the right = 001000 2 = 8 Shifting is really multiplying or dividing by a power of 2 –32 >> 2 = 8 2^5 / 2^2 = –32 / 4 = 8 = 2^3
33
Back to the mouse If you remember the mouse example, the y coordinate and the button info were far away from the lowest order bit You can get the real y position and button position with shifting and masks
34
Summary What computer memory looks like What a pointer is Pointer arithmetic Bitwise operations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.