DEV Community

Ahaiwe Emmanuel
Ahaiwe Emmanuel

Posted on • Updated on

Data Structure and Endianness in Nodejs

Data structure is the collection of data values, the relationship among them and the functions or operations that can be applied to the data according to Wikipedia.

Memory in Data structure
The concept of memory is that there are finite memory slots. So it can be exhausted.
The program is always stored in a free memory slot. Memory is stored as bit and byte. 8 bits make a byte.

For example:

1 represented as a byte is 0000 0001         
2 represented as a byte is 0000 0010
3 represented as a byte is 0000 0011
4 represented as a byte is 0000 0100
Enter fullscreen mode Exit fullscreen mode

These are represented in base 2. And that gives us a finite amount of numbers we can store because base 2^8 = 256. So to save bigger numbers, we increase the bytes.

In java for example, "int" is the equivalent of 4 bytes, 32 bits integer. Type "long" represents 8 bytes, a 64 bits integer.

Endianness in Data Structure
The concept of Endianness in computer memory storage refers to how bytes are read. It could either be the smallest byte value first called Little-endian (read from left to right) or biggest byte value first called big-endian (read from left to right).
For example,

65,000 decimal number in base 2 is: 0b11111101 11101000.
0b here helps us know it is in base 2 and not 1,111,110,111,101,000
Enter fullscreen mode Exit fullscreen mode

The answer above is in big-endian byte because the byte with the biggest 0b11111101 is written first reading from left to right. We can confirm this by converting the big-endian padded by another byte of zeros "0b1111110100000000 to base 10" on google. The result is 64,768 which is very close to our initial number of 65,000.

For networks, the standard is Big-endian and for most PCs, the standard is Little-endian. Check this code snippet online to find out what endian your machine runs. My machine runs on Little-endian.

Endianness has largely ceased to be a matter of concern because of modern computer languages removing this needless complexity and processors which are Bi-endian and can handle both.

For further studies, please visit https://www.algoexpert.io

Discussion (0)