Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect one's or two's complement architecture in C++?

What is the most reliable way to detect whether the architecture uses one's or two's complement representation in C++?

like image 839
Vincent Avatar asked May 11 '13 19:05

Vincent


People also ask

Does C use 2's complement?

C and C++ as specified, however, are not two's complement. Signed integers currently allow the existence of an extraordinary value which traps, extra padding bits, integral negative zero, and introduce undefined behavior and implementation-defined behavior for the sake of this extremely abstract machine.

What is 2's complement in computer architecture?

A two's-complement number system encodes positive and negative numbers in a binary number representation. The weight of each bit is a power of two, except for the most significant bit, whose weight is the negative of the corresponding power of two.


1 Answers

You shouldn't have to worry - there aren't too many ones complement machines out there :)

But the easiest thing might be to compare "-1" with ~0.

like image 147
paulsm4 Avatar answered Oct 22 '22 01:10

paulsm4