-
Notifications
You must be signed in to change notification settings - Fork 0
size_of_using_bitwise
uint8_t size_of_using_bitwise()
{
uint8_t size = 0;
uint8_t x = 1;
while (x) {
x <<= 1;
size++;
}
return size / 8;
}The size_of_using_bitwise function is intended to calculate the size of a data type in bytes using bitwise operations. However, it actually returns a constant value based on the size of a uint8_t (which is always 1 byte). The while loop shifts x left until it becomes 0, counting the number of shifts.
-
Initial Values:
size = 0-
x = 1(binary00000001)
-
While Loop:
- The loop continues as long as
xis not 0. - Each iteration shifts
xleft by 1 bit and incrementssize.
- The loop continues as long as
-
Left Shifts:
-
x <<= 1shifts the bit pattern ofxone position to the left. - This continues until
xbecomes 0 (after 8 shifts).
-
-
Return Value:
- The function returns
size / 8which is8 / 8 = 1byte.
- The function returns
A char is typically 1 byte (8 bits). The loop shifts x left 8 times, resulting in:
Initial:
x = 00000001
size = 0
Iteration 1:
x <<= 1 -> 00000010
size++
Iteration 2:
x <<= 1 -> 00000100
size++
Iteration 3:
x <<= 1 -> 00001000
size++
Iteration 4:
x <<= 1 -> 00010000
size++
Iteration 5:
x <<= 1 -> 00100000
size++
Iteration 6:
x <<= 1 -> 01000000
size++
Iteration 7:
x <<= 1 -> 10000000
size++
Iteration 8:
x <<= 1 -> 00000000
size++
Result: size = 8, size / 8 = 1 byte
A float is typically 4 bytes (32 bits). However, the size_of_using_bitwise function always returns 1, which is incorrect for types larger than 1 byte.
Consider a custom struct:
typedef struct {
int a;
float b;
char c;
} CustomStruct;If we manually determine the size:
-
int(typically 4 bytes) -
float(typically 4 bytes) -
char(typically 1 byte) - Total size (including padding) is 12 bytes.
The size_of_using_bitwise function would still incorrectly return 1 byte for this struct because it is based on the size of a uint8_t.
Initial:
x = 00000001
size = 0
Iterations:
x <<= 1 -> 00000010, size++
x <<= 1 -> 00000100, size++
x <<= 1 -> 00001000, size++
x <<= 1 -> 00010000, size++
x <<= 1 -> 00100000, size++
x <<= 1 -> 01000000, size++
x <<= 1 -> 10000000, size++
x <<= 1 -> 00000000, size++
Result: size = 8, size / 8 = 1 byte
Initial:
x = 00000001
size = 0
Iterations (the function won't actually run for the struct):
x <<= 1 -> 00000010, size++
x <<= 1 -> 00000100, size++
x <<= 1 -> 00001000, size++
x <<= 1 -> 00010000, size++
x <<= 1 -> 00100000, size++
x <<= 1 -> 01000000, size++
x <<= 1 -> 10000000, size++
x <<= 1 -> 00000000, size++
Result: size = 8, size / 8 = 1 byte
The size_of_using_bitwise function is not versatile enough to determine the size of data types other than uint8_t. It won't work correctly for struct, typedef, union, or larger types like float.
To handle these, we need a more robust approach, such as using sizeof operator directly in C:
printf("Size of char: %lu bytes\n", sizeof(char));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of CustomStruct: %lu bytes\n", sizeof(CustomStruct));This provides accurate sizes for all types, including custom structs.