endianness - java numeric type conviersion when the underlying platform is big endian -
i'm wondering happens when write following java code on big-endian platform:
int = 1; byte b = (byte)a;
on little-endian platform, layout of variable a's 4 bytes x01000000, converting a byte, still x01, makes b equals 1. however, on big-endian platform, laid out x00000001, b still equal x01? if does, how magic happen?
operations performed in registers in cpu, not in memory. registers neither big-endian nor little-endian , not randomly accessible.
in case, behaviour defined in java language specification keeping lower bits in each case, wouldn't matter how cpu works.
how magic happen?
no magic, cpu discards higher bits. (technically cpu may sign extension many cpus have 32-bit or 64-bit registers) how bytes stored in memory before did doesn't matter.
Comments
Post a Comment