-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.

2
  • 2
    Possible duplicate of How Do Computers Work? Commented Nov 26, 2018 at 10:40
  • I just wanna know how the values are occupying the allocated memory rather than brief answers. Commented Nov 27, 2018 at 3:28

2 Answers 2

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

2
  • 2
    you've only got 8 bits (one byte) in 00000100 Commented Nov 26, 2018 at 21:30
  • Yes I agree @timB33 but I need how it is storing the numbers in the 16 boxes.Exactly I need graphical representation Commented Nov 27, 2018 at 3:32
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.