Podcast
Questions and Answers
Which of the following is NOT typically covered when discussing how integers are represented?
Which of the following is NOT typically covered when discussing how integers are represented?
- Two's complement
- Floating point precision (correct)
- Integer ranges
- One's complement
In C, the size of an int
is guaranteed to be the same across all platforms.
In C, the size of an int
is guaranteed to be the same across all platforms.
False (B)
What operator in C is used to determine the size of a data type or variable?
What operator in C is used to determine the size of a data type or variable?
sizeof
_________ is transforming subtraction into addition by the negation of y with 1.
_________ is transforming subtraction into addition by the negation of y with 1.
Match the data type with its minimum size in C:
Match the data type with its minimum size in C:
When assigning a value from a larger integer type to a smaller integer type, what process may occur?
When assigning a value from a larger integer type to a smaller integer type, what process may occur?
Integer expansion of an unsigned integer involves setting the unused bits to the sign bit.
Integer expansion of an unsigned integer involves setting the unused bits to the sign bit.
What is the name of the process where unused bits are set to the same value as the sign bit during integer expansion?
What is the name of the process where unused bits are set to the same value as the sign bit during integer expansion?
When a value from a signed integer type is assigned to an unsigned integer type, the value is simply _________ as is.
When a value from a signed integer type is assigned to an unsigned integer type, the value is simply _________ as is.
In C, if an expression mixes signed and unsigned integers, what typically happens to the signed values?
In C, if an expression mixes signed and unsigned integers, what typically happens to the signed values?
Java avoids pitfalls in integer manipulation by using the two's complement representation.
Java avoids pitfalls in integer manipulation by using the two's complement representation.
Give an example of computer hardware where unsigned integers are commonly used.
Give an example of computer hardware where unsigned integers are commonly used.
_________ is translating a value from space-efficient to computation-efficient format.
_________ is translating a value from space-efficient to computation-efficient format.
Match the terms with their descriptions in the context of integer manipulation:
Match the terms with their descriptions in the context of integer manipulation:
If you have an n-bit signed integer X (where n is not 8, 16, 32, or 64), how can you determine if X is negative?
If you have an n-bit signed integer X (where n is not 8, 16, 32, or 64), how can you determine if X is negative?
Bitwise operations should generally be avoided when trying to optimize the performance of your code.
Bitwise operations should generally be avoided when trying to optimize the performance of your code.
Explain how subtraction can be performed using bitwise operations and addition.
Explain how subtraction can be performed using bitwise operations and addition.
In the context of unpacking integers, _________ refers to unpacking an integer from least-significant bit to most-significant bit.
In the context of unpacking integers, _________ refers to unpacking an integer from least-significant bit to most-significant bit.
What is the result of the expression 127 + 1
when both numbers are treated as 8-bit signed integers?
What is the result of the expression 127 + 1
when both numbers are treated as 8-bit signed integers?
Integer truncation always results in a loss of information.
Integer truncation always results in a loss of information.
What is the purpose of 'unpacking' and 'packing' integers?
What is the purpose of 'unpacking' and 'packing' integers?
The ______
is a CPU that allows to use same math operation for both signed and unsigned integers due to the way it stores integer.
The ______
is a CPU that allows to use same math operation for both signed and unsigned integers due to the way it stores integer.
Given the code:
char x = -7;
short y = x;
What will be the value of y
?
Given the code:
char x = -7;
short y = x;
What will be the value of y
?
When assigning an int
to a char
, only the least significant byte of the int
is considered; the rest are ignored.
When assigning an int
to a char
, only the least significant byte of the int
is considered; the rest are ignored.
In C, the sizeof
operator returns the size of a variable or data type in what unit?
In C, the sizeof
operator returns the size of a variable or data type in what unit?
Using a(n) _________ integer allows the range of numbers to be doubled as there are no negative numbers.
Using a(n) _________ integer allows the range of numbers to be doubled as there are no negative numbers.
How can you unpack a signed integer from a char array in little-endian format?
How can you unpack a signed integer from a char array in little-endian format?
When unpacking an n-bit integer from a source to an m-bit destination where m > n, you only need to consider whether the integer is signed or unsigned, but not the endianness.
When unpacking an n-bit integer from a source to an m-bit destination where m > n, you only need to consider whether the integer is signed or unsigned, but not the endianness.
In unpacking, what does the term 'endianness' refer to?
In unpacking, what does the term 'endianness' refer to?
When unpacking a 'big-endian' integer, you must first unpack the _________ to get the proper data.
When unpacking a 'big-endian' integer, you must first unpack the _________ to get the proper data.
Given that x
is -9876, what could the maximum value of ux
be unsigned short ux = (unsigned short) x;
?
Given that x
is -9876, what could the maximum value of ux
be unsigned short ux = (unsigned short) x;
?
Mixing signed and unsigned integers in comparison operations can often lead to incorrect results.
Mixing signed and unsigned integers in comparison operations can often lead to incorrect results.
Why do most modern systems use two's complement representation for signed integers?
Why do most modern systems use two's complement representation for signed integers?
During __________ , a smaller integral type is assigned to a larger integral type.
During __________ , a smaller integral type is assigned to a larger integral type.
What happens when a signed char with the value of -2 is assigned to an int?
What happens when a signed char with the value of -2 is assigned to an int?
The sizeof a data type will not change depending on where you run your program.
The sizeof a data type will not change depending on where you run your program.
On the Timberlea system mentioned in the content, how many bytes does a short
occupy?
On the Timberlea system mentioned in the content, how many bytes does a short
occupy?
The most-significant bit(MSB) in the signed integer tells whether the integer is _________.
The most-significant bit(MSB) in the signed integer tells whether the integer is _________.
In general, why is unpacking and packing integers useful?
In general, why is unpacking and packing integers useful?
For modern systems, it is faster to determine whether 0 > -1 rather than (unsigned int)0 > -1
For modern systems, it is faster to determine whether 0 > -1 rather than (unsigned int)0 > -1
An equivalent of the term unpacking that means the opposite direction would be:
An equivalent of the term unpacking that means the opposite direction would be:
Data must flow between a destination that may or may not be of sizes _______.
Data must flow between a destination that may or may not be of sizes _______.
Flashcards
Integer Representation
Integer Representation
Integers are represented using one's complement, two's complement, signed vs unsigned, with specified ranges.
Bit Stream Manipulation
Bit Stream Manipulation
Unpack or pack integers from or to bit streams
int
int
Is the word size of the machine in C and is platform dependent.
sizeof Operator
sizeof Operator
Signup and view all the flashcards
Unsigned Integer Range
Unsigned Integer Range
Signup and view all the flashcards
Unsigned Integer Addition
Unsigned Integer Addition
Signup and view all the flashcards
Unsigned Integer Subtraction
Unsigned Integer Subtraction
Signup and view all the flashcards
Two's Complement
Two's Complement
Signup and view all the flashcards
Why Use Two's Complement?
Why Use Two's Complement?
Signup and view all the flashcards
Integer Truncation
Integer Truncation
Signup and view all the flashcards
Integer Expansion (Unsigned)
Integer Expansion (Unsigned)
Signup and view all the flashcards
Integer Expansion Sign
Integer Expansion Sign
Signup and view all the flashcards
Little-endian
Little-endian
Signup and view all the flashcards
Big Endian
Big Endian
Signup and view all the flashcards
Unpacking
Unpacking
Signup and view all the flashcards
Packing
Packing
Signup and view all the flashcards
set() function
set() function
Signup and view all the flashcards
reset() function
reset() function
Signup and view all the flashcards
Study Notes
Integer Representation (CSCI 1120)
- Integers can be represented using one's complement and two's complement
- Representation types include signed vs unsigned integers
- Considers representation of integer ranges and addition/multiplication
Integer Manipulation (CSCI 2122)
- The course discusses how to manipulate integers
- Systems use two's complement
- Considers mixing signed and unsigned integers
- Mixing integers of different sizes requires understanding
- Discusses how to unpack and pack integers
Lecture Outcomes
- Ability to unpack/pack integers from/to bit streams
- Ability to manipulate integer values at the bit level
- Considerations for mixing signed and unsigned integers are to be identified
Integer Types in C
- Most languages offer several kinds of integers
- Integer sizes vary based on the platform in C
Minimum Sizes
char
is at least 1 byte.short
is at least 2 bytes.int
is the word size of the machine.long
is at least 4 bytes.long long
is at least 8 bytes.
Integer Ranges on a system
- Integer types may be larger than the minimum size on some systems
- It's possible for
int
,long
, andlong long
to be the same size - Unsigned
char
range: 0 to 255 or 0 to (2^8 - 1) - Signed
char
range: -128 to 127 or -2^7 to (2^7 - 1) - Unsigned
short
range: 0 to 65536 or 0 to (2^16 - 1) - Signed
short
range: -32768 to 32767 or -2^15 to (2^15 - 1) - Unsigned
int
range: 0 to 4294967295 or 0 to (2^32 - 1) - Signed
int
range: -2147483648 to 2147483647 or -2^31 to (2^31 - 1) - Unsigned
long
range: 0 to 18446744073709551615 or 0 to (2^64 - 1) - Signed
long
range: -9223372036854775808 to 9223372036854775807 or -2^63 to (2^63 - 1)
Determining Integer Size
- Applications may need to know the size of an integer
sizeof
Operator
- Used to determine the size of a type
- Takes a type or a value
- Returns the number of bytes the type or value occupies
- It's used extensively when allocating memory
char
size: 1 byteshort
size: 2 bytesint
size: 4 byteslong
size: 8 bytes
Importance of Integer Manipulation
- Essential for programs to input and write binary data, including integers
- Important for interacting with hardware, like graphics cards
- Critical for translating binary data between different hardware and software
- Necessary when using integers as components of data structures, like bitmaps
- It's required to implement high-level numerical packages from integers at a low level
Unsigned Integers
- For an n-bit integer, the range is 0 to 2^n - 1
- Value is calculated as: bn-1 * 2^(n-1) + bn-2 * 2^(n-2) + ... + b1 * 2^1 + b0 * 2^0 where bi is the i-th bit in the int
- Addition operates bitwise with carry
- Subtraction is transformed into addition by adding the negation of y with 1 (x - y -> x + ~y + 1)
Two's Complement Examples (8-bit)
- -1 is represented: 1 = 00000001, -1 = (~1 + 1) = ~00000001 + 00000001 = 11111110 + 00000001 = 11111111 = 0xff
- -(-1) is represented: -1 = 11111111, -(-1) = (~-1 + 1) = ~11111111 + 00000001 = 00000000 + 00000001 = 00000001 = 0x01
- -0 is represented: 0 = 00000000, -0 = (~0 + 1) = ~00000000 + 00000001 = 11111111 + 00000001 = 00000000 = 0x00
- 127 + 1 is represented: 127 = 01111111 = 0x7f, 127 + 1 = 01111111 + 00000001 = 10000000 = -128 = 0x80
Usage of Two's Complement
- Signed integer representations, such as one's complement and signed-magnitude, are alternatives
- Two's complement is used because other representations have two representations for 0
- The CPU can use the same operations for both signed and unsigned integers if two's complement is used
- For two's complement all operations are the same, except expansion (copying an integer value from a smaller integral type to a larger one)
- All modern CPUs use two's complement
Problems with Signed Integers
- Tasks includes how to determine if an n-bit signed integer X (where n is not 8, 16, 32, or 64) is negative or positive
- Write a C function to determine if X < Y (without using <)
Integer Casting
- Occurs when assigning an integer between smaller/larger integral types
- When assigning a value from a larger integral type to a smaller one, the value may be truncated
Integer Truncation
- Values assigned from a larger integral type to a smaller one may be truncated
- When assigning an n-bit integral type to an m-bit integral type where n > m, the n-m most significant bits are dropped and only the m least significant bits are considered
Integer Expansion: Unsigned
- Assigned from a smaller integral type to a larger integral type, the unused bits are zeroed
- When assigning from an n-bit integral type to a m-bit integral type, where m > n, the m-n most significant bits are 0
Integer Expansion: Signed
- Assigned from a smaller integral type to a larger integral type, the unused bits are set to the sign bit (sign-extended)
- When assigning from an n-bit to an m-bit integral type, with m > n, the m-n most significant bits are set to the same value as the sign bit
- This is called sign-extension
Integer Conversion
- Assigned from a signed integral type to an unsigned integral type, the value is simply copied as is
Pitfalls of Casting
- Evaluating the bit-pattern of the int -9876 as Unsigned Binary gives: → 55660
- With an unsigned to 2sC prints ux = 4294967295, x = some value with a bit pattern
Implicit Casting
- Signed values are implicitly cast to unsigned in single expression when mixing them
- Includes comparison operators
Potential Issues With Unsigned Comparisons
- Comparison to
0U
means-1
has the bit-pattern corresponding toULONG_MAX
, so the comparison is false - Comparing
2147483647U > -2147483647 - 1
means the right-hand side is implicitly cast as unsigned, resulting in a false comparison - However,
2147483647 > (int) 2147483647U
treats the right-hand side as signed (-1), leading to a true comparison
Handling Unsigned and Negative Numbers
- When using unsigned ints it can create bugs if not handled carefully
- When mixing signed and unsigned values in comparisons, unexpected results may arise because of implicit casting (e.g.,
i <= length - 1
)
Avoiding Issues from Signedness
- Java chose to use only signed integers to avoid these pitfalls
Downside to Signed Only
- Half the range of integers may be lost if negative numbers are not needed
- Many hardware devices use unsigned integers (e.g., hard disks and SSDs)
- More complex code may be needed to use signed integers where unsigned would be appropriate otherwise
- Most integers in systems are unsigned
Decomposing Integers
- Given a 4-byte integer, it can be decomposed into its 4 bytes in little-endian order
Application for Integer Packing
- Integer representation is optimized for speed during execution, not space
- A 64-bit integer containing the value 1 takes a lot of space
Efficient Space Allocation
- Many applications require more efficient use of space
- Include communication protocols, device interfaces, and database format
- Space-efficient and computation-efficient formats necessitates efficient conversion for use
Unpacking
- Translates a value from a space-efficient to a computation-efficient format
Packing
- Translates a value from a computation-efficient to a space-efficient format
Unpacking Process
- Process translates from source to destination
- Considers a source as an n-bit integer (n may not be 8, 16, 32, or 64 bits)
- The end point is an m-bit integer, where m > n (if possible) and it is typically 8, 16, 32, or 64
Unpacking Considerations
- Integer can be unsigned or signed (typically two’s complement)
- If the source larger or smaller than the destination
- Integer will need to be expanded if smaller
- Integer will need to be truncated if larger
- the source is little endian or big endian has to be checked
- With Little-endian unpack integer least-significant to most-significant bit
- With Big-endian unpack integer most-significant to least-significant bit
Unpacking Function
- Implementing unpacking function with Parameters includes pointer to the bytes containing value and n is the number of bits
- It returns a long long containing integer.
- The approach includes initializing result to the appropriate sign extension and copying over the bits from least significant to most significant.
SET() and RESET() Methods
- Sets a specified bit
- Takes a long long v (64 bit signed value) and integer i
Set Implementation
- Implemented with the code return v | (1
The Packing Function
- To implement the packing function Parameters needs to be passed which is integer to be packed, and bytes containing packing
- Check to see if bytes has little endian
- Then the number of bits required to pack the integer is returned
Determine the Packed Integer
- Determine size of integer by reducing sign extension to the minimum 0000000000000000000000000000000000000000000000000000000000000001
- Copy over the bits from least significant to most significant
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore integer representation using one's complement and two's complement. Learn to manipulate integers, understanding signed vs. unsigned types and different sizes in C. Master bit-level operations and packing/unpacking.