-4

In C program I'm doing below stuff

int x  = 4;

Let us assume integer has 2 bytes in this case. So my question here is the variable x will hold two bytes that mean 16 bits. So here how the value 4 will be stored in 16 bits ?

Till now my understanding is the value 4 will be converted to Hex so it results [0x0004] and this hex value is stored in the 16 bit boxes ?

I dont know whether my understanding is correct but I need how the value of the x is mapped to 16 bit boxes ? It would be good if anyone provide structural/graphical representation about storing values in memory.

Abdulvakaf K
  • 101
  • 3
  • 2
    Possible duplicate of [How Do Computers Work?](https://softwareengineering.stackexchange.com/questions/81624/how-do-computers-work) – gnat Nov 26 '18 at 10:40
  • I just wanna know how the values are occupying the allocated memory rather than brief answers. – Abdulvakaf K Nov 27 '18 at 03:28

2 Answers2

2

If you've got a 16bit OS then there'll be 2 bytes next to each other in memory. One with 00000100 in and the other with 00000000. Whether it's stored as 00000000-00000100 or 00000100-00000000 depends on whether your OS is little-endian or big-endian. See: https://en.wikipedia.org/wiki/Endianness

timB33
  • 191
  • 1
  • 7
1

The exact representation is unspecified in the C standard, (which is partly why int might be 16, 32 or 64 bits)

A typical representation is 2's complement, because it allows the same hardware to do both signed and unsigned arithmetic.

Decimal 4 is binary 0000000000000100. There is no "conversion", all values are bit strings.

Caleth
  • 10,519
  • 2
  • 23
  • 35