JS takes me to explore computer principle – memory representation of int type

Time:2020-3-14

When learning C + + variable types, I suddenly want to deeply explore the memory structure of each type
So, he turned his attention to JS
That’s because… Arraybuffer typearray these APIs are simply customized for my study!
ECMA, you are so close

Target is int integer
It’s said that he’s a 32-bit complement
Use his scalpel to see what-1 looks like

//C + + int takes up 4 bytes, so open a 4 byte (32 bit) storage space first
let buff = new ArrayBuffer(4);
//Then I want to see his complement when I type in signed integer
//In buff is used to receive input. The input is signed int
let in_buff = new Int32Array(buff);  
//Out buff for output
let out_buff = new Uint32Array(buff); 
//Then it's time to witness the miracle and see what the - 1 complement looks like
in_buff[0] = -1;
Console. Log ('hexadecimal output ', out Fu buff [0]. ToString (16). Padstart (8,0));
Console. Log ('binary output ', out Fu buff [0]. ToString (2). Padstart (32,0));
Console. Log ('memory structure ', new uint8array (buff));

Browser F12, a wave on the console
PS: JS is easy to use. You can find any computer in the village, open the browser, and there will be a running environment, and there will be cli, hehe, love

JS takes me to explore computer principle - memory representation of int type

WOW! -1’s complement is so much f in hexadecimal and so much 1 in binary. It’s very complementary at first sight, and it’s right at first sight.

-2, – 3100
No problem

JS takes me to explore computer principle - memory representation of int type

Conclusion:
Type int is represented by complement
First bit of complement is symbol bit, positive 0 negative 1
The rest are numerical digits
Positive number: directly corresponding to its binary number;
Negative number: reverse the value by bit and then + 1, that is to say, add the number that he can just enter the 33rd place (his other half, with her, the number can be complete, can be sublimated)

Recommended Today

Sublime text 2 syntax based configuration file

Recently, I was learning python programming language, but I encountered a small problem. The original Ruby coding specification is indented with two spaces, so it was previously set in the global user configuration of sublime”tab_size”: 2Therefore, when editing Python files, you have to set it from the menu every timetab_sizeThe size of the is 4. […]