Unit-4 Signed or Unsigned Bits Concept
Unit-4 Signed or Unsigned Bits Concept
In computing, data types can be either signed or unsigned, which determines how the bits in the data
type are interpreted.
Signed Bit: A signed bit indicates whether a number is positive or negative. In a signed integer, the
leftmost bit (also known as the most significant bit) is used as the sign bit. If the sign bit is 0, the
number is positive. If the sign bit is 1, the number is negative.
Unsigned Bit: An unsigned bit does not have a sign bit, so all bits in the data type are used to
represent the magnitude of the number. This means that unsigned integers can represent larger
positive values than their signed counterparts but cannot represent negative numbers.
1) char is a data type in many programming languages, including C and C++, used to represent characters. In C and
C++, char is a single-byte data type that can hold one character from the character set, which is typically the ASCII or
UTF-8 character set.
In addition to representing characters, char can also be used to store small integers within the range of -128 to 127
(if signed) or 0 to 255 (if unsigned). This dual nature of char allows it to be used for both character manipulation and
basic arithmetic operations.
char myInt = 65; // Assigning an integer value (ASCII value for 'A')
char is commonly used for storing individual characters, creating strings, and in situations where memory efficiency
is important, such as when dealing with large arrays of characters.
2) short is a data type in C and C++ (and other languages) that is used to store integer values. It is typically smaller in
size than an int but larger than a char. In most implementations, a short is 2 bytes in size.
The range of values that can be stored in a short depends on whether it is signed or unsigned. For a signed short, the
range is typically -32,768 to 32,767, while for an unsigned short, the range is 0 to 65,535.
In this example, myShort is a variable of type short that stores the value 1000.
3) int is a data type in C and C++ (and other languages) that is used to store integer values. It is typically the most
natural size for integers on a given platform, often matching the word size of the underlying hardware (e.g., 32 bits
on a 32-bit system, 64 bits on a 64-bit system).
The range of values that can be stored in an int also depends on whether it is signed or unsigned. For a signed int,
the range is typically -2,147,483,648 to 2,147,483,647, while for an unsigned int, the range is 0 to 4,294,967,295.
Here is an example of declaring and using an int variable in C:
int myInt = 42; In this example, myInt is a variable of type int that stores the value 42.
3) long is a data type in C and C++ (and other languages) that is used to store integer values similar to int, but with a
larger size. On most platforms, a long is larger than an int, typically 4 or 8 bytes depending on the system
architecture.
The range of values that can be stored in a long also depends on whether it is signed or unsigned. For a signed long,
the range is typically at least -2,147,483,648 to 2,147,483,647 (same as int on many platforms), while for an unsigned
long, the range is typically 0 to 4,294,967,295 (same as unsigned int on many platforms).
In this example, myLong is a variable of type long that stores the value 1234567890. The L at the end of the number
is a suffix indicating that the number should be treated as a long literal.
4) long long is a data type in C and C++ (and other languages) that is used to store integer values that require a larger
range than int or long. It is typically larger than both int and long, with at least 64 bits of precision.
The range of values that can be stored in a long long also depends on whether it is signed or unsigned. For a signed
long long, the range is typically at least -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, while for an
unsigned long long, the range is typically 0 to 18,446,744,073,709,551,615.
In this example, myLongLong is a variable of type long long that stores the value 123456789012345. The LL at the
end of the number is a suffix indicating that the number should be treated as a long long literal.
5) Local Variable:
A local variable, in the context of programming, is a variable that is declared inside a function or a block of code and
is only accessible within that function or block. Local variables have local scope, which means they are only visible
and usable within the function or block in which they are declared.
Local variables are typically used to store temporary data that is needed only within a specific function or block of
code. Once the function or block completes execution, the local variables are destroyed, and their memory is
released.
#include <stdio.h>
void myFunction() {
int main() {
// printf("%d\n", localVar); // This would cause an error because localVar is not accessible here
return 0;
}
In this example, localVar is a local variable within the myFunction function and is not accessible outside of that
function.
Wrap-around:
Wrap-around, in the context of programming and computer science, refers to the behavior that occurs when a
mathematical operation on a data type exceeds the maximum value that can be represented by that data type.
Instead of throwing an error or causing undefined behavior, the value "wraps around" to the minimum value of the
data type (for signed integers) or to zero (for unsigned integers).
For example, consider an 8-bit unsigned integer with a maximum value of 255. If you add 1 to 255, the result would
be 0, because the value has wrapped around:
Similarly, for signed integers, if you add 1 to the maximum positive value, the result will be the minimum value of
that data type due to wrap-around:
Wrap-around can lead to unexpected behavior in programs if not handled properly, especially in situations where
the programmer assumes that overflow will result in an error or some other form of termination.
Caller and Callee:
In programming, "caller" and "callee" are terms used in the context of function or method calls, especially
in languages like C, C++, and assembly language.
Caller: The caller is the part of the program that invokes (calls) a function or method. It initiates the
function call and passes any necessary arguments to the callee. After the callee completes its execution,
control usually returns to the caller.
Callee: The callee is the function or method that is called by the caller. It receives the arguments passed by
the caller, performs its tasks, and then usually returns a value to the caller.
In summary, the caller initiates the call to a function, and the callee executes the code within that function.
For example
#include <iostream>
// Callee function
void sayHello() {
std::cout << "Hello, World!" << std::endl;
}
// Caller function
int main() {
std::cout << "Calling the sayHello function..." << std::endl;
sayHello(); // Call to the sayHello function
std::cout << "Returned from the sayHello function." << std::endl;
return 0;
}
In this example, main is the caller, as it calls (invokes) the sayHello function. sayHello is the callee, as it is
the function being called. When main calls sayHello, it passes control to sayHello, which executes its code
(in this case, printing "Hello, World!"). After sayHello completes, control returns to main, which then prints
"Returned from the sayHello function."
Consider an example in ARM assembly language. Suppose we have a 32-bit register r0 containing the value
0x12345678 (binary: 0001 0010 0011 0100 0101 0110 0111 1000).
The resulting value is 0x45678000. This value is still a 32-bit value with the lower 16 bits set to 0, not a 16-bit value.
To convert this to a 16-bit value, you would need to use additional operations, such as shifting or masking, to extract
the lower 16 bits.
***************
To convert a 32-bit value to a 16-bit value, you need to decide on the method of truncation or reduction. One
common method is to simply discard the higher-order bits, keeping only the lower 16 bits.
# 32-bit value
value_32_bit = 0xDEADBEEF
In this example, 0xFFFF is a bitmask that preserves only the lower 16 bits of the original 32-bit value. The result will
be the value 0xBEEF in this case.
**************
-0x80000000 is the hexadecimal representation of the minimum value for a 32-bit signed integer in two's
complement format. In decimal, this value is -2147483648.
+0x7fffffff is the hexadecimal representation of the maximum value for a 32-bit signed
integer in two's complement format. In decimal, this value is 2147483647.
In two's complement representation, to subtract 1 from a negative number, you add 1 to its absolute value and then
negate the result.