❔ How can I save a byte in a ushort?
I'm a game design and development student and I'm currently studying for my c# test (I'm new to coding). I was doing this exercise but there is a part where I have to save a byte in a ushort but I don't understand how I can save it?? The exercise is the following:
"Ask the user for two two-digit numbers (bytes), calculate their multiplication, which must be stored in an ushort, and display the result on the screen."
I appreciate the help ❤️ 😦
28 Replies
What do you have so far?
From the wording, I'm not sure you are really being asked to convert, but in any case the conversion from byte to ushort is implicit, so you can simply assign the result of the multiplication to a variable of type ushort.
This is what I got but I'm not really sure if what I´m doing makes sense (sry if its in spanish):
byte x, y;
Console.WriteLine("Enter a two-digit value:");
x = Convert.ToByte(Console.ReadLine());
Console.WriteLine("Enter another two-digit value");
y = Convert.ToByte(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("La multiplicación de los valores es " + x * y);
Console.WriteLine("\n");
ushort
is an unsigned byte, 8bits can't be saved into 7bits (the "missing" bit determines if the number is positive or negative). The reverse is true thothank you ❤️
ushort s = (ushort)(a | (b << 8));
if you need to save 2 bytes in an ushort
if you need to multiply, just do
ushort s = (ushort)(a * b);
ooooooooooooooooooooooooooooooooh
i think i understand
tyy so much
when doing math with types like byte and ushort, the math is implicitly done as int math, so the result will be an
int
but you can just cast it to ushort
its not possible for byte*byte multiplication to overflow ushort so its perfectly safeokay
i hope i don't fail this test i'll do my best
@Akseli I'm not sure why I thought you could do what I suggested. Sorry for spreading misinformation my defense, I was tired.
The statement "ushort is an unsigned byte" is not correct at all. A byte is an 8 bit quantity. A short is a 16 bit quantity.
byte
is actually an unsigned byte and sbyte
is signed byte in c#sbyte...probably the least used type in c# 🙂
hmmm
pretty much yeah
besides mtreit's statement, integers don't have a sign bit, unlike floating numbres
it is pretty useless, only time i see it being used is when some people marshalling strings but i dont think you even need to use it for that, byte should work aswell
sbyte guards you from integer overflow
what
what
int reserves one bit for the sign, uint doesnt
nope
1111 is -1
you don't even always need different operations
e. g. sum is the same
The most significant bit is the sign bit in twos complement, what do you mean there isn't a sign bit?
but in floats it works conceptually differently
well, you could probably call it such
but it isn't really interpreted most of the time as it works in floats
I've messed up, sorry
I mean, good point
you can probably think of it as of a sign bit
🤔
2s complement
I've always heard it called the sign bit 🤷♂️
same
i guess we blew up this thread
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.