Chapter 2 - Representing and Manipulating Information Flashcards
What is a byte?
The smallest unit of addressable memory.
What base does hexadecimal use?
Base 16.
Why is it useful to write bit patterns with hexadecimal rather than binary or decimal?
Binary can be too “verbose” and decimal makes it difficult to convert to and from bit patterns.
If a binary number does not contain a multiple of 4 bits, how should the bits be grouped?
From right to left so that the leftmost group is the smallest.
What is the hexadecimal equivalent of the following binary number?
11 1100 1010 1101 1011 0011
11 1100 1010 1101 1011 0011
3 C A D B 3
i.e. 0x3CADB3.
What is the bit pattern associated with the hexadecimal representation 0x25B9D2?
0x25B9D2 has bit pattern:
0010 0101 1011 1001 1101 0010
What is the hexadecimal representation for the binary pattern 1010 1110 0100 1001?
1010 1110 0100 1001 has hex representation 0xAE49.
If a number has the form x=2^N, how many zeros will it contain in its bit pattern after the leading 1?
N zeros.
How can a number of the form x=2^N be easily written straight into hex?
By determining i and j such that N=i+4j. There will be j zeros after the nonzero hex character and i determines the nonzero hex character (which is given by 2^i).
For example, 2^11 has N=3+4*2, and 2^3=8, so 2^11=0x800.
Do the numbers below match?
N (decimal): 5
2^N (hex): 0x20.
Yes.
N=5=i+4j=1+4*1 (and 2^1=2), so 2^N in hex is 0x20.
Do the numbers below match?
N (decimal): 23
2^N (hex): 0x400000.
No.
N=23=i+4j=3+4*5 (and 2^3=8), so 2^N in hex is 0x800000.
Do the numbers below match?
N (decimal): 15
2^N (hex): 0x8000.
Yes.
N=15=i+4j=3+4*3 (and 2^3=8), so 2^N in hex is 0x8000.
Do the numbers below match?
N (decimal): 12
2^N (hex): 0x1000.
Yes.
N=12=i+4j=0+4*3 (and 2^0=1), so 2^N in hex is 0x1000.
Do the numbers below match?
Binary: 1001 1110
Hex: 0x9E
Yes.
Do the numbers below match?
Binary: 1001 0001
Hex: 0x91
Yes.
Do the numbers below match?
Binary: 1010 1110
Hex: 0xAE
Yes.
Do the numbers below match?
Binary: 0111 0101
Hex: 0x73
No.
Is the following correct?
0x605C + 0x5 = 0x606A
No. 0x605C + 0x5 = 0x6061.
Is the following correct?
0x605C + 32 = 0x607C
Yes. 0x605C + 0x20 = 0x607C.
What is the result of the expression?
0x60FA - 0x605C = ???
0x60FA - 0x605C = 0x9D.
What important system parameter indicates the nominal size of pointer data?
The word size.
What is the size of the virtual address space of a machine with a w-bit word size?
The machine can access address 0 to 2^w-1 so there are 2^w addressable bytes.
What is the virtual address space limit of a machine with a 32-bit word size?
4GB.
True or false? 64-bit machines can run 32-bit programs.
True.
How can the program prog.c be compiled with gcc so that it runs on both 32-bit and 64-bit machines?
With the command:
$ gcc -m32 prog.c
Can the program resulting from the command
$ gcc -m64 prog.c
be run on a 32-bit machine?
No. It has been compiled as a 64-bit program so it can only run on a 64-bit machine.
What size will a pointer used in the program prog.c, compiled as below have?
$ gcc -m32 prog.c
A pointer uses the full word size of the program so the pointer will be 4 bytes in size.
What is the term used to describe a system where the bytes are ordered from least significant to most significant?
Little endian.
What is the term used to describe a system where the bytes are ordered from most significant to least significant?
Big endian.
Can a system be configured as little endian or big endian?
Typically, no, but more recent microprocessor chips are “bi-endian” and can be configured as either.
Where do the terms little and big endian come from?
From Gullivers Travels by Jonathan Swift, where two warring factions could not settle on how a hard-boiled egg should be open – by the little end or the big.
When might the ordering of bytes become important? (i.e. the little- or big-endian-ness.)
In the communication of binary data over a network between different machines.
What would the outputs of the following code be on a little endian machine?
int a=0x12345678;
unsigned char* ap=(unsigned char*) &a;
show_bytes(ap,1);
show_bytes(ap,2);
show_bytes(ap,3);
Here, show_bytes(p,N) is a function that prints the first N bytes at a given location.
int a=0x12345678; unsigned char* ap=(unsigned char*) &a; show_bytes(ap,1); // = 78 show_bytes(ap,2); // = 78 56 show_bytes(ap,3); // = 78 56 34
What would the outputs of the following code be on a big endian machine?
int a=0x12345678;
unsigned char* ap=(unsigned char*) &a;
show_bytes(ap,1);
show_bytes(ap,2);
show_bytes(ap,3);
Here, show_bytes(p,N) is a function that prints the first N bytes at a given location.
int a=0x12345678; unsigned char* ap=(unsigned char*) &a; show_bytes(ap,1); // = 12 show_bytes(ap,2); // = 12 34 show_bytes(ap,3); // = 12 34 56
What is the hex code for the decimal digit 1 in ASCII? What would the byte representation of “12345” look like in memory?
The ASCII code for the decimal digit 1 is 31 in hex. Thus, “12345” is given by 31 32 33 34 35.
True or false? Text data is more platform independent than binary data.
True.
What would be printed as a result of the following call to show_bytes?
const char m = “mnopqr”;
show_bytes((unsigned char) m, strlen(m));
Note that letters ‘a’ through ‘z’ have ASCII codes 0x61 through 0x7A.
const char *m = "mnopqr"; show_bytes((unsigned char*) m, strlen(m)); // Note: m is the 13th letter so it starts with 0x60+0xD=0x6D. // The full sequence of bytes is 6D 6E 6F 70 71 72.
What are the symbols uses to denote Boolean algebra operations?
NOT: ~
AND: &
OR: |
EXCLUSIVE-OR: ^
Suppose a=[01001110] and b=[11100001]. What is “a^b”?
The character ^ is used to denote the exclusive-or operation so, given a=[01001110] and b=[11100001], a^b=[10101111].
What term is used to describe the operation where one bit pattern is used to select bits from another bit pattern?
Bit masking.
Write C expressions, in terms of variable x, for the following values. The code should work for any word size w ≥ 8. For reference, the result of evaluating the expressions for x = 0x87654321, with w = 32, is shown.
A. The least significant byte of x, with all other bits set to 0. [0x00000021]
B. All but the least significant byte of x complemented, with the least significant byte left unchanged. [0x789ABC21]
C. The least significant byte set to all ones, and all other bytes of x left unchanged.
[0x876543FF]
Reference: x = 0x87654321.
A. x & 0xFF [0x00000021]
B. x ^ (~0xFF) [0x789ABC21]
C. x | 0xFF [0x876543FF]
What is a simple way to multiply a variable x by 2^N?
By using the bit shift x<
Do bit shift operations associate left to right or right to left? For example, what is the order of operations in the expression x «_space;j «_space;k?
Bit shift operations associate left to right so x «_space;j «_space;k is equivalent to (x «_space;j) «_space;k.
Using only bit-level and logical operations, write a C expression that is equivalent to x == y. In other words, it will return 1 when x and y are equal and 0 otherwise.
(x^y) will have entry 1 wherever x and y differ and entry 0 wherever x and y are the same. Thus, x == y can be represented by the expression !(x^y).
What two types of right shift are there?
Logical and arithmetic.