Signed integer int<8> in header

Hello, I defined a custom header which contains the signed integer.

header custom_h{
int<8> node0;
int<8> node1;
}

I also define the metadata which also includes the signed integer.

struct metadata_t{
int<32>tem0;
int<32>tem1;
}

After some computation for the meta.tem0 and meta.tem1; we assume the value of tem0 and tem1 will be in the range of (-128, 127), i.e., using 8bit can represent them exactly.
Then, I need to give the value of the meta.tem0 and meta.tem1 to the custom header.

apply{
hdr.custom.node0 = (int<8>) meta.tem0;
hdr.custom.node1 = (int<8>) meta.tem1;
}

However, this may cause unexpected result because A slice of a signed integer is treated as
an unsigned integer. Does this mean that if meta.tem0 is a negative number, node0 will get a positive number after casting?

The code you have written does not use the slice operator at all. It is casting an expression with type int<32> to int<8>.

I would recommend testing your program with a few values, including the extremes of -128, -1, 0, 1, and 127, and if you find any surprising behavior, please ask.