C
C#9mo ago
Moods

how Int32 etc. store their values

So we know that stuff like Int and long is just a sequence of bits with the length depending on the data type. I wanted to know at what level is this conversion to binary being done like if I have
int foo = 90;
Console.WriteLine(foo);
int foo = 90;
Console.WriteLine(foo);
To my knowledge this 90 is converted to its 32-bit binary equivalent and stored then, and when you’d want to read from that variable (I’m assuming correct me if I’m wrong) this binary is then converted to its decimal equivalent and then displayed with the WriteLine function. I just wanted to know is this conversion done at a hardware level (like how processors have instructions built in) or is it done by the compiler. And what of the case with negative numbers because those have to be stored as two’s complements too
6 Replies
Angius
Angius9mo ago
There's no conversion, as none is needed A number is a number, notation doesn't matter Nice decimal representations are only for us, humans to read and write The computer only works in binary But, whether it's 0b0110 or 6... it's the same number
Moods
MoodsOP9mo ago
Yeeeah I know that and all I was just curious as to what level it's done I know it's all neatly abstracted for us but I'm curious
Angius
Angius9mo ago
I would assume at the runtime level, but I'm honestly not sure Never wondered about such technicalities People in #allow-unsafe-blocks or #roslyn might know
mtreit
mtreit9mo ago
The numeric value is never in 'decimal' form, as @ZZZZZZZZZZZZZZZZZZZZZZZZZ pointed out. The computer never works in 'decimal'. For that particular example, the value 90 is written into the assembly at compile time. What is written is the 4 bytes that represent that number.
Moods
MoodsOP9mo ago
Yeah I knew the first part already. the folks in the channel ZZZZZZZ… pointed out explained the details behind it
mtreit
mtreit9mo ago
Actually technically it looks like if the value is small enough it will be written as a single byte in the assembly, not 4 bytes, so presumably there is some optimization to save space. Oh I guess they explained that part in #allow-unsafe-blocks
Want results from more Discord servers?
Add your server