1

Say that I am comparing an int and a short int. According to the second edition of "The C Programming Language" written on page 173 the short int will be converted into an int, which is 4 bytes instead of the short integers 2 bytes on my system.

My question is: How will this be done on memory level? Will the short integer get 2 bytes added to it or will the data be copied to a completely different memory location?

I just edited from comparing double and int to short and int. Apologize for inconvenience.

Donkey King
  • 87
  • 1
  • 8
  • Floats are encoded completely differently than integers. Even if you were converting between an integer type and a floating point type of the same size, the resulting floating point would consist of different bits. It's not a straightforward bitwise operation. – Kevin Feb 21 '17 at 21:27
  • You shouldn't think of it in terms of representation, but in terms of value. If the value of the `int` fits into the admissible range of type `double` everything is fine. If it doesn't, you are in trouble. – Jens Gustedt Feb 21 '17 at 21:29
  • 1
    it's implementation defined. some machines do load-load-convert-compare, load-convert-compare, load-convert-save-compare, some do load-load-compare.... load double > load int > int to double > double-double compair might be the most commonly used now, since FPUs are mostly load-store and register-register addressing. – user3528438 Feb 21 '17 at 21:31
  • When it says converted, it is only talking about a temporary value used for the comparison. The original variable in memory is unchanged. – stark Feb 21 '17 at 21:52

2 Answers2

1

For most cases (omitting some obscure microcontrollers) there will be no memory needed at all. Everything will be done on register level. Even if type being processed cannot be held in single register it will be split into multiple registers and processed accordingly.

Anty
  • 1,486
  • 1
  • 9
  • 12
0

Comparisons of this type usually take place between hardware registers in the CPU. Your memory value will be loaded into a register with the conversion taking place in the load.

You will get a sequence something like this:

CVTWL MEMORY_LOCATION, RO COMPL RO, R1

user3344003
  • 20,574
  • 3
  • 26
  • 62