-5

Example:

int max = a > b ? a : b;
int min = a + b - max;

What determines whether this will work? The processor? The hardware? The language? Help me understand this at as deep a level as possible.

user5648283
  • 5,913
  • 4
  • 22
  • 32

3 Answers3

2

The processor IS the hardware (at least for the purposes of this question).

The language is purely a way for you to express things in such a way as to allow it to convert it to what the processor itself expects. The role of the language here would be to define what "int" means, what arithmetic operators are/do, and what their exceptional behavior is. In the low-level languages (like C/C++), it leaves several things to be "implementation defined", like the overflow behavior of integers. Other languages (like Python) may define "int" to be an abstract (not a hardware) concept and thereby change some of the rules (like detecting overflows and doing custom behavior).

If the language leaves something implementation defined and the implementation offloads that decision to the hardware, then the hardware is what defines the behavior of your code.

iAdjunct
  • 2,739
  • 1
  • 18
  • 27
0

Computers(hardware) represent numbers in two's complement. Check this for details of two's complement, and why computers use it.

In two's complement signed numbers(not floating ones for now, for sake of simplicity) have a sign bit as most significant bit. For example:

01111111

Represents 127 in two's complement. And

10000000

represents -128. In both example, the first bit is sign bit, if it's 0, then the number is positive, else negative.

8-bit signed numbers can represent numbers between -128 and 127, so if you add 127 and 3, you won't get 130, you will get -126 because of overflow. Let's see why:

 01111111
 00000011
+________
 10000010 which is a negative number, -126 in two's complement.

How hardware understand if an overflow occurred? In addition for example, if you add two positive numbers and the result gets negative, it means overflow. And if you add two negative numbers and result gets positive it means overflow again.

I hope that would be a nice example for how these things are happening in hardware level.

Ferit
  • 8,692
  • 8
  • 34
  • 59
0

The high level programming language provides a way for humans to describe what they want to happen. A compiler reduces that down into a language the processor understands, (ultimately) machine code. The instruction set for a particular processor is designed to be useful for doing tasks, general purpose processors for general purpose tasks including the ones you have described. Unlike pencil and paper math where if we need another column another power of ten, 99+1 = 100 for example two digits wide going in, 3 digits coming put. Processors have a fixed with for their registers, that doesnt mean you cant get creative, but the language and the resources (memory, disk space, etc) have limits. And the processor either directly in the logic or the compiler implementing the right sequence of instructions, can and will detect an overflow if you ask it to, in general. Some processors harder than others and some processors are not general purpose enough, but I dont think we need to worry about those, the one you are reading this web page in definitely can handle this.

old_timer
  • 69,149
  • 8
  • 89
  • 168