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
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
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 numberYeeeah 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
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
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.
Yeah I knew the first part already. the folks in the channel ZZZZZZZ… pointed out explained the details behind it
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