C
C#2y ago
anamari

❔ 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
hiyosilver
hiyosilver2y ago
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.
anamari
anamari2y ago
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");
Instinct
Instinct2y ago
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 tho
anamari
anamari2y ago
thank you ❤️
Akseli
Akseli2y ago
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);
anamari
anamari2y ago
ooooooooooooooooooooooooooooooooh i think i understand tyy so much
Akseli
Akseli2y ago
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 safe
anamari
anamari2y ago
okay i hope i don't fail this test i'll do my best
hiyosilver
hiyosilver2y ago
@Akseli I'm not sure why I thought you could do what I suggested. Sorry for spreading misinformation waaaaaaaaaahhhhhh my defense, I was tired.
mtreit
mtreit2y ago
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.
Akseli
Akseli2y ago
byte is actually an unsigned byte and sbyte is signed byte in c#
mtreit
mtreit2y ago
sbyte...probably the least used type in c# 🙂
Akseli
Akseli2y ago
hmmm pretty much yeah
WhiteBlackGoose
besides mtreit's statement, integers don't have a sign bit, unlike floating numbres
Akseli
Akseli2y ago
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
WhiteBlackGoose
sbyte guards you from integer overflow
Akseli
Akseli2y ago
what
WhiteBlackGoose
what
Akseli
Akseli2y ago
int reserves one bit for the sign, uint doesnt
WhiteBlackGoose
nope 1111 is -1 you don't even always need different operations e. g. sum is the same
mtreit
mtreit2y ago
The most significant bit is the sign bit in twos complement, what do you mean there isn't a sign bit?
WhiteBlackGoose
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
Instinct
Instinct2y ago
I've messed up, sorry catsweat
WhiteBlackGoose
I mean, good point you can probably think of it as of a sign bit 🤔
uint int

1111 = -0001
0111 = +0111
1000 = -1000
uint int

1111 = -0001
0111 = +0111
1000 = -1000
Akseli
Akseli2y ago
2s complement
mtreit
mtreit2y ago
I've always heard it called the sign bit 🤷‍♂️
Akseli
Akseli2y ago
same i guess we blew up this thread
Accord
Accord2y ago
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.