Skip to content

size_of_using_bitwise

Mallikarjunarao Kosuri edited this page Jul 14, 2024 · 1 revision

size_of_using_bitwise Function

uint8_t size_of_using_bitwise()
{
    uint8_t size = 0;
    uint8_t x = 1;
    while (x) {
        x <<= 1;
        size++;
    }
    return size / 8;
}

Explanation

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.

Step-by-Step Process

  1. Initial Values:

    • size = 0
    • x = 1 (binary 00000001)
  2. While Loop:

    • The loop continues as long as x is not 0.
    • Each iteration shifts x left by 1 bit and increments size.
  3. Left Shifts:

    • x <<= 1 shifts the bit pattern of x one position to the left.
    • This continues until x becomes 0 (after 8 shifts).
  4. Return Value:

    • The function returns size / 8 which is 8 / 8 = 1 byte.

Example with char and float

For char:

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

For float:

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.

Custom Struct Example

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.

Using the Function:

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.

Textual representation of char and CustomStruct

char Example:

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

Custom Struct Example:

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

Limitations

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.

Clone this wiki locally