Podcast
Questions and Answers
In C, the size of integer data types is consistent across all platforms.
In C, the size of integer data types is consistent across all platforms.
False (B)
What operator in C is used to determine the number of bytes a data type occupies?
What operator in C is used to determine the number of bytes a data type occupies?
sizeof
Subtraction can be transformed into addition by adding the ________ of y with 1.
Subtraction can be transformed into addition by adding the ________ of y with 1.
negation
Match the following integer types in C with their minimum size:
Match the following integer types in C with their minimum size:
What is the result of 127 + 1
when using 8-bit two's complement?
What is the result of 127 + 1
when using 8-bit two's complement?
One's complement arithmetic is universally used by modern CPUs.
One's complement arithmetic is universally used by modern CPUs.
What is the primary reason modern CPUs use two's complement representation?
What is the primary reason modern CPUs use two's complement representation?
When assigning a value from a larger integer type to a smaller integer type, the value may be ________.
When assigning a value from a larger integer type to a smaller integer type, the value may be ________.
Match the operation with its description:
Match the operation with its description:
What happens during unsigned integer expansion when assigning a smaller integer type to a larger one?
What happens during unsigned integer expansion when assigning a smaller integer type to a larger one?
In signed integer expansion (sign-extension), the most significant bits are set to zero.
In signed integer expansion (sign-extension), the most significant bits are set to zero.
What is the term for setting the unused bits to the same value as the sign bit?
What is the term for setting the unused bits to the same value as the sign bit?
When a value from a signed integral type is assigned to an unsigned integral type, the value is ________ as is.
When a value from a signed integral type is assigned to an unsigned integral type, the value is ________ as is.
Match the term with its definition:
Match the term with its definition:
What is the term used when translating a value from space-efficient to computation-efficient format?
What is the term used when translating a value from space-efficient to computation-efficient format?
Little-endian format unpacks integers from most-significant to least-significant bit.
Little-endian format unpacks integers from most-significant to least-significant bit.
What is the name of the format that unpacks integer most-significant to least-significant bit?
What is the name of the format that unpacks integer most-significant to least-significant bit?
In unsigned integer subtraction, if length is zero, then length - 1
equals ______.
In unsigned integer subtraction, if length is zero, then length - 1
equals ______.
Which of the following is a typical use case for unsigned integers but NOT for signed integers?
Which of the following is a typical use case for unsigned integers but NOT for signed integers?
In C, if there's a mix of unsigned and signed integers in a single expression, what happens?
In C, if there's a mix of unsigned and signed integers in a single expression, what happens?
If -1 < 0U
in C, the expression evaluates to true.
If -1 < 0U
in C, the expression evaluates to true.
What value is the integer variable x
when it is cast from unsigned integer 4294967295?
What value is the integer variable x
when it is cast from unsigned integer 4294967295?
When assigning a value from a signed integer type to an unsigned integer type, a negative number is simply ______ as is.
When assigning a value from a signed integer type to an unsigned integer type, a negative number is simply ______ as is.
Match the endianness with the bit order:
Match the endianness with the bit order:
What is a key consideration for using signed integers when unsigned would otherwise suffice?
What is a key consideration for using signed integers when unsigned would otherwise suffice?
Interacting with graphics cards never requires manipulation of binary data.
Interacting with graphics cards never requires manipulation of binary data.
What is the C operator used to perform a bitwise OR operation?
What is the C operator used to perform a bitwise OR operation?
The result name of function to set the ith bit to 1 of a 64 bit signed value is ___________.
The result name of function to set the ith bit to 1 of a 64 bit signed value is ___________.
When unpacking an integer using little-endian format, which byte is processed first?
When unpacking an integer using little-endian format, which byte is processed first?
The following C code results in 55660
. Why does this happen?
short x = -9876;
unsigned short ux = (unsigned short) x;
printf("x = %d, ux = %u\n", x, ux);
The following C code results in 55660
. Why does this happen?
short x = -9876;
unsigned short ux = (unsigned short) x;
printf("x = %d, ux = %u\n", x, ux);
Which of the following accurately describes the role of n
in the unpack_signed
function?
Which of the following accurately describes the role of n
in the unpack_signed
function?
In the context of the pack_signed
and unpack_signed
functions, what does 'src' stand for?
In the context of the pack_signed
and unpack_signed
functions, what does 'src' stand for?
Using twos complement allows CPU to use the same operations code for signed and unsigned integers.
Using twos complement allows CPU to use the same operations code for signed and unsigned integers.
What is the decimal representation of 0xff
using 8-bit ints?
What is the decimal representation of 0xff
using 8-bit ints?
In C-programming, when assigning an n
-bit integral type to m
-bit integral type, if n > m
, the _______ bits are dropped.
In C-programming, when assigning an n
-bit integral type to m
-bit integral type, if n > m
, the _______ bits are dropped.
Given that x is 0x1234cafe
, what is the value of z after running the code below?
int x = 0x1234cafe\;
char z = x\;
Given that x is 0x1234cafe
, what is the value of z after running the code below?
int x = 0x1234cafe\;
char z = x\;
Match the C type with the number of bytes in memory:
Match the C type with the number of bytes in memory:
What is one of the key reasons to 'unpack' the integers?
What is one of the key reasons to 'unpack' the integers?
Java adopted signed/unsigned integers in the style of C/C++ to directly reflect the underlying hardware.
Java adopted signed/unsigned integers in the style of C/C++ to directly reflect the underlying hardware.
What are the values of s
and i
after the following code has completed?
What are the values of s
and i
after the following code has completed?
The bit-pattern of -1
corresponds to ________.
The bit-pattern of -1
corresponds to ________.
Flashcards
Why use Two's Complement?
Why use Two's Complement?
Integers represented with only one value for zero. The CPU can use the same operations for both signed and unsigned integers.
What is the sizeof
operator?
What is the sizeof
operator?
The operator in C used to determine the size of a type or variable in bytes.
What is integer truncation?
What is integer truncation?
Assigning a value from a larger integer type to a smaller one, which may result in losing some data.
What is integer expansion?
What is integer expansion?
Signup and view all the flashcards
What is Unpacking?
What is Unpacking?
Signup and view all the flashcards
What is Packing?
What is Packing?
Signup and view all the flashcards
What is Little-Endian Unpacking?
What is Little-Endian Unpacking?
Signup and view all the flashcards
What is Big-Endian Unpacking?
What is Big-Endian Unpacking?
Signup and view all the flashcards
What does set() do?
What does set() do?
Signup and view all the flashcards
What does reset() do?
What does reset() do?
Signup and view all the flashcards
Mixing signed and unsigned takeaways?
Mixing signed and unsigned takeaways?
Signup and view all the flashcards
Study Notes
- In CSCI 1120, integer representation topics included one's complement, two's complement, signed vs. unsigned integers, integer ranges, and addition/multiplication.
- CSCI 2122 focuses on manipulating integers, covering why systems use two's complement, considerations for mixing signed/unsigned integers, mixing integers of different sizes, and unpacking/packing integers.
Integers in Programming
- Most languages offer various integer types.
- Integer sizes in C depend on the platform.
char
is at least 1 byte.short
is at least 2 bytes.int
matches the machine's word size.long
is at least 4 bytes.long long
is at least 8 bytes.- Some systems allow
int
,long
, andlong long
to be the same size.
Type | Unsigned Range | Unsigned Max Formula | Signed Range | Signed Range Formula |
---|---|---|---|---|
char |
0 to 255 | 0 to (2^8 - 1) | -128 to 127 | -2^7 to (2^7 - 1) |
short |
0 to 65536 | 0 to (2^16 - 1) | -32768 to 32767 | -2^15 to (2^15 - 1) |
int |
0 to 4294967295 | 0 to (2^32 - 1) | -2147483648 to 2147483647 | -2^31 to (2^31 - 1) |
long |
0 to 18446744073709551615 | 0 to (2^64 - 1) | -9223372036854775808 to 9223372036854775807 | -2^63 to (2^63 - 1) |
Determining Integer Size
- The
sizeof
operator determines the size of a type. - It takes either a type or a value as input.
- It returns the number of bytes the type or value occupies.
- It is used during memory allocation.
- On Timberlea:
char
= 1 byte,short
= 2 bytes,int
= 4 bytes,long
= 8 bytes.
Importance of Integer Manipulation
- Programs must input/output binary data and integers.
- Programs interact with hardware like graphics cards.
- Programs translate binary data between different hardware/software.
- Programs use integers in data structures like bitmaps.
- Programs implement high-level numerical packages from low-level integers.
Unsigned Integers Review (n-bit integer)
- Range: 0 to 2^n - 1
- A value will be b(n-1)2^(n-1) + b(n-2)2^(n-2) +...+b(1)2^1+b(0)2^0 , where bi is the ith bit.
- Addition uses bitwise operations with carry.
- Subtraction: converts to addition using the negation of 'y' plus 1: x - y becomes x + ~y + 1.
Two's Complement Examples (8-bit integers)
- To determine the representation of -1: 1 = 00000001, -1 = ~1 + 1 = ~00000001 + 00000001 = 11111110 + 00000001 = 11111111 = 0xff
- To determine the representation of -(-1): -1 = 11111111, -(-1) = ~-1 + 1 = ~11111111 + 00000001 = 00000000 + 00000001 = 00000001 = 0x01
- To determine the representation of -0: 0 = 00000000, -0 = ~0 + 1 = ~00000000 + 00000001 = 11111111 + 00000001 = 00000000 = 0x00
- To determine the representation of 127 + 1: 127 = 01111111 = 0x7f, 127 + 1 = 01111111 + 00000001 = 10000000 = -128 = 0x80
Why Use Two's Complement?
- Although, other signed integer representations include one's complement and Signed-Magnitude, two's complement has advantages.
- The above methods have two representations for 0
- CPUs can use the same operations for both signed and unsigned integers with two's complement.
- Operations are the same, except for expansion when copying from a smaller to a larger type.
- All modern CPUs use two's complement.
Problems
- Given an n-bit signed integer X where n isn't 8, 16, 32, or 64, determine if X is negative or positive.
- Without using '<', determine if X < Y and write a C function for this.
- Write functions for equal, not_equal, less_than_or_equal, greater_than_or_equal, and greater_than.
Casting Integers
- What happens when assigning an integer from a smaller to a larger type, or vice versa?
- Also, what results from assigning a signed integer to an unsigned integer or vice versa?
Integer Truncation
- Assigning a larger integral type value to a smaller integral type may truncate the value.
- When assigning an n-bit integer to an m-bit integer type where n > m, the n-m most significant bits are dropped and only the 'm' least significant bits are considered.
Integer Expansion (Unsigned)
- When assigning a value from a smaller integral type to a larger one, the unused bits turn into zeroes.
- Assigning from an n-bit integer to an m-bit integer where m > n, the 'm-n' most significant bits are 0.
Integer Expansion (Signed)
- Assigning from a smaller integral type to a larger type, the unused bits are set to the sign bit (sign-extension).
- Process: Assigning an n-bit integer to an m-bit integer type (m > n), the m-n most significant bits match the sign bit.
- This is sign-extension.
Integer Conversion
- Assigning a signed integral type value to an unsigned type simply copies the value.
Casting Surprises: 2'sC to Unsigned
- Short x = -9876; unsigned short ux = (unsigned short) x;
printf("x = %d, ux = %u\n", x, ux);
prints x = -9876, ux = ?- The bit-pattern of +9876 in 16 bits is 0010 0110 1001 0100.
- -9876 is 1101 1001 0110 1100.
- Interpreting -9876's bit-pattern as an unsigned binary gives 55660.
Casting Surprises: Unsigned to 2'sC
uint32_t ux = ULONG_MAX;
int32_t x = (int) ux;
printf("ux = %u, x = %d", ux, x);
- Prints ux = 4294967295, x = ...
Casting Surprises Takeaways
- Mixing signed and unsigned values in an expression implicitly casts signed values to unsigned.
- This includes comparison (<, >, ==, <=, >=).
-1 < 0U
results in -1 being cast as unsigned.- A past slide shows a negative one will have a bit-pattern corresponding to ULONG_MAX
2147483647U > -2147483647-1)
RHS casts to unsigned for the bit pattern1...31zeros... → 2^31
- Hence the above evaluates to false.
2147483647 > (int) 2147483647U
- The right-hand side is treated as signed
- A right Hand side with a bit patter of -1, the above expression evaluates to to True.
Pitfalls of Unsigned Integers
- Consider the following code:
int sum(int *values, unsigned int length) {
int s = 0;
for (int i = 0; i <= length - 1; i++) {
s += values[i];
}
return s;
}
- If length is 0,
length - 1
becomes a large number (4 billion), which can cause a crash. - Solution: Use
i < length
as bound.
unsigned int foo(char *x) { ... }
char *foomax(char *x, char *y) {
if (foo(x) - foo(y) > 0) {
return x;
} else {
return y;
}
}
- The code demonstrates the problems that can occur when using unsigned integers. The expression turns what may intuitively seem negative to unsigned integers, and thus positive.
- Could write expression
foox(x) > foo(y)
Why Not Always Use 2's Complement?
- Java adopted this approach to avoid the discussed pitfalls.
- Drawbacks: lose half the integer range when negative numbers aren't needed, many hardware devices depend on unsigned integers (SSDs), need more complex code for signed integers where unsigned are appropriate, and most system integers are unsigned.
Problem: Decomposing an Integer (Little Endian)
void decompose(int x, char *bytes) {
for (int i = 0; i < 4; i++) {
bytes[i] = x;
x = x >> 8;
}
}
- The code takes a 4-byte integer and turns the values into its 4 constituent bytes
- It demonstrates accessing each byte by shifting and assigning.
Application: Unpacking and Packing Integers
- Integer representation is optimized for speed, not space.
- E.g. a 64-bit integer with value 1 consumes a lot of space:
00000...0001
- Space-efficient and Computation efficiency requires unpacking/packing operations.
- Unpacking: converts a value from space-efficient to computation-efficient format
- Packing: converts a value from computation-efficient to space-efficient format
Unpacking
- Unpacking translates from source to destination with the following considerations:
- Source of n-bit integer where n may not be 8, 16, 32, or 64 bits
- Destination of m-bit integer with m > n (typical 8, 16, 32, or 64 bits)
- Check if the integer is signed or unsigned (two's complement).
- If source is smaller, the integer will need to be expanded
- If source is larger, the integer will need to be truncated.
- Little-endian: unpack integer least-significant to most-significant bit.
- Big-endian: unpack integer most-significant to least-significant bit.
Problem: Unpack an Integer
- Using an input of long long unpack_signed(char *buffer, int n):
- Where the parameters: bytes that contain a little Endian value, the amount of bits
- Write an algorithm to return a long long integer with a signed value
long long unpack_signed(char *buffer, int n) {
long long result = 0; /* assume positive int */
if (test(buffer[(n - 1) / 8], (n - 1) % 8)) {
result = ~result;
}
/* assume n <= 64 */
for (int i = 0; i < n; i++) {
result = reset(result, i);
if (test(buffer[i / 8], i % 8)) {
set(result, i);
}
}
return result;
}
Set and Reset Functions
long long set(long long v, int i) {
return v | (1 << i);
}
long long reset(long long v, int i) {
return v & ~(1 << i);
}
-
set() Function:
- Takes:
- Long long v: a 64-bit signed value.
- Integer i: the position of the bit.
- Returns:
- The given values with bit
i
set to1
.
- The given values with bit
- Takes:
-
reset() Function:
- Takes:
- Long long v: a 64-bit signed value.
- Integer i: the position of the bit.
- Returns:
- The given value with bit
i
set to0
.
- The given value with bit
- Takes:
Packing
- Packing: Translates from source to destination.
- The source is an 'm'-bit integer.
- The destination is an 'n'-bit integer (n may not be 8, 16, 32, or 64 bits).
- Considerations consist of weather the integers signed(twos complement)
- The destination uses little or big endian
int pack_signed(long long src, char *buffer) {
int n = 63; /* assume full size int */
int sign = test(src, n);
for (n = 63; n > 0; n--) {
if (test(src, n - 1) != sign)
break;
}
for (int i = 0; i <= n; i++) {
buffer[i >> 3] = reset(buffer[i >> 3], i & 7);
if (test(src, i)) {
buffer[i >> 3] = set(buffer[i >> 3], i & 7);
}
}
return n; /* Feature: Need 1 bit to encode 0 or 1 */
}
Problem: Packing an Integer
int pack_signed(long long src, char *buffer)
- Packing an Integer implementation with the following parameters:
src
: the value to be packed, (long long) integer that could be signed*buffer
: pointer to the bytes containing the packing (little endian)
- Returns, the number of bits to pack the integer
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore integer representation in computer systems, covering one's complement, two's complement, and signed vs. unsigned integers. Learn about integer ranges, addition/multiplication, and considerations for mixing signed/unsigned integers of different sizes. Understand unpacking and packing integers.