-5

I have very general question about how computers work with numbers.

In general computer systems only know binary - 0 and 1. So in memory any number is a sequence of bits. It does not matter if the number represented is a int or float.

  1. But when does things like floating-point-numbers based on IEEE 754 standard and the twos-complement enter the game? Is this only a thing of the compilers (C/C++,...) and VMs (.NET/Java)?

  2. Is it true that all integer numbers are represented by using the twos-complement?

  3. I have read about CPUs that use co-processors for performing the floating-point-arithmetic. To tell a CPU to use it special assembler commands exists like add.s (single precision) and add.d (double precision). When I have some C++ code where a float is use, will such assembler commands be in the output?

I am totally confused at the moment. Would be great if you can help me with that.

Thank you! Stefan

Skully
  • 347
  • 2
  • 4
  • 19
  • One question per question please. – Paul R Aug 12 '15 at 08:25
  • Here's an article on the topic (which keeps getting mentioned on SO regularly): http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Dmitry Grigoryev Aug 12 '15 at 08:28
  • @DmitryGrigoryev This article does not answer this question at all. – Pascal Cuoq Aug 12 '15 at 08:30
  • Well, it covers the IEEE 754 standard. [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) and [BCD](https://en.wikipedia.org/wiki/Binary-coded_decimal) are well covered in wikipedia. – Dmitry Grigoryev Aug 12 '15 at 08:34
  • Sorry for posting several questions in one post. I have forget about this. Thanks for the article! I am sure it is a good literature about this topic. – Skully Aug 12 '15 at 08:44

1 Answers1

1

In general computer systems only know binary - 0 and 1. So in memory any number is a sequence of bits. It does not matter if the number represented is a int or float.

This is correct for the representation in memory. But computers execute instructions and store data currently being worked on in registers. Both instructions and registers are specialized, for some of them, for representations of signed integers in two's complement, and for others, for IEEE 754 binary32 and binary64 arithmetics (on a typical computer).

So to answer your first question:

But when does things like floating-point-numbers based on IEEE 754 standard and the twos-complement enter the game? Is this only a thing of the compilers (C/C++,...) and VMs (.NET/Java)?

Two's complement and IEEE 754 binary floating-point are very much choices made by the ISA, which provides specialized instructions and registers to deal with these formats in particular.

Is it true that all integer numbers are represented by using the twos-complement?

You can represent integers however you want. But if you represent your signed integers using two's complement, the typical ISA will provide instructions to operate efficiently on them. If you make another choice, you will be on your own.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • Registers are not really specialized, the same register can hold two's complement and BCD numbers just fine. FPU registers can hold IEEE 754 numbers and packed two's complement intergers. – Dmitry Grigoryev Aug 12 '15 at 08:39
  • 1
    @DmitryGrigoryev as I thought the answer made clear, the registers are specialized in that there are instructions that make computations expressed in terms of the IEEE 754 floating-point number represented by the bits that are in them, or in terms of the signed integers represented in two's complement in them. In fact, I don't know what I should change to make it clearer, but downvote away if you think it's a bad answer. – Pascal Cuoq Aug 12 '15 at 08:41
  • My point is that instructions are specialized, not registers. On x86, if you execute `fadd`, the value in FP0 will be interpreted as IEEE754, and if you execute `paddb`, the same register will be interpreted as packed bytes in two's complement. Yet another instruction will interpret the same register as unsigned. – Dmitry Grigoryev Aug 12 '15 at 09:36
  • @Dmitry Grigoryev: this may be true for x86 architecture. I have a book called Computerarchitektur by Andrew Tanenbaum where it says on page 374 that it depends on the system. Some computers also have separate registers for decimals and integers to perform any instruction. – Skully Aug 12 '15 at 11:39