Thiago Adams
2024-08-07 11:28:09 UTC
How cast works?
Does it changes the memory?
For instance, from "unsigned int" to "signed char".
Is it just like discarding bytes or something else?
For instance, any 4 bytes type, cast to 2 bytes type is just the lower 2
bytes?
[A][B][C][D]
->
[A][B]
I also would like to understand better signed and unsigned.
There is no such think as "signed" or "unsigned" register, right?
How about floating point?
The motivation problem.
I have a union, with unsigned int, unsigned char etc.(all types)
I need to execute a cast in runtime (like script).
The problem is that this causes an explosion of combinations that I am
trying to avoid.
---------------------------------------------------------
enum type {
TYPE_UNSIGNED_CHAR,
TYPE_UNSIGNED_INT,
};
struct number {
enum type type;
union U {
unsigned int unsigned_int_value;
unsigned char unsigned_char_value;
} data;
};
struct number cast_to(enum type t, const struct number* n) {
struct number r = {0};
r.type = t;
r.data = n->data;
//maybe fill with zeros the extra bytes?
return r;
}
// All combinations...
struct number cast_to2(enum type t, const struct number* n)
{
struct number r = {0};
r.type = n->type;
switch (t)
{
case TYPE_UNSIGNED_CHAR:
switch (n->type)
{
case TYPE_UNSIGNED_CHAR:
r.data.unsigned_char_value = n->data.unsigned_char_value;
break;
case TYPE_UNSIGNED_INT:
r.data.unsigned_char_value = n->data.unsigned_int_value;
break;
}
break;
case TYPE_UNSIGNED_INT:
switch (t)
{
case TYPE_UNSIGNED_CHAR:
r.data.unsigned_int_value = n->data.unsigned_char_value;
break;
case TYPE_UNSIGNED_INT:
r.data.unsigned_int_value = n->data.unsigned_int_value;
break;
}
break;
}
return r;
}
Does it changes the memory?
For instance, from "unsigned int" to "signed char".
Is it just like discarding bytes or something else?
For instance, any 4 bytes type, cast to 2 bytes type is just the lower 2
bytes?
[A][B][C][D]
->
[A][B]
I also would like to understand better signed and unsigned.
There is no such think as "signed" or "unsigned" register, right?
How about floating point?
The motivation problem.
I have a union, with unsigned int, unsigned char etc.(all types)
I need to execute a cast in runtime (like script).
The problem is that this causes an explosion of combinations that I am
trying to avoid.
---------------------------------------------------------
enum type {
TYPE_UNSIGNED_CHAR,
TYPE_UNSIGNED_INT,
};
struct number {
enum type type;
union U {
unsigned int unsigned_int_value;
unsigned char unsigned_char_value;
} data;
};
struct number cast_to(enum type t, const struct number* n) {
struct number r = {0};
r.type = t;
r.data = n->data;
//maybe fill with zeros the extra bytes?
return r;
}
// All combinations...
struct number cast_to2(enum type t, const struct number* n)
{
struct number r = {0};
r.type = n->type;
switch (t)
{
case TYPE_UNSIGNED_CHAR:
switch (n->type)
{
case TYPE_UNSIGNED_CHAR:
r.data.unsigned_char_value = n->data.unsigned_char_value;
break;
case TYPE_UNSIGNED_INT:
r.data.unsigned_char_value = n->data.unsigned_int_value;
break;
}
break;
case TYPE_UNSIGNED_INT:
switch (t)
{
case TYPE_UNSIGNED_CHAR:
r.data.unsigned_int_value = n->data.unsigned_char_value;
break;
case TYPE_UNSIGNED_INT:
r.data.unsigned_int_value = n->data.unsigned_int_value;
break;
}
break;
}
return r;
}