Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

design spec: Are integers in zig guaranteed to have two's complement binary representation? #1723

Closed
andrewrk opened this issue Nov 15, 2018 · 2 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Nov 15, 2018

Thanks to this reddit post for the question.

On one hand, it may be nice to define integers to work this way, since this is probably how it will work on every machine. It would certainly be the least surprising. On the other hand, in the spirit of "communicate intent precisely", it isn't that hard to use appropriate functions for "serializing" integers as twos complement. This puts fewer limitations on the optimizer, and potentially could let us do some interesting safety features, with unused bits.

Note that currently according to the language reference, floats are guaranteed to have IEEE binary representation. The resolution to this issue probably applies to floats as well.

The language specification (#75) depends on this issue.

Note: status quo: integers are always stored in twos complement binary representation.

@andrewrk andrewrk added this to the 1.0.0 milestone Nov 15, 2018
@AronParker
Copy link

I'd just like to add (I'm the guy who asked on reddit) that there is a C++ proposal to make integers two's complement which will probable be implemented in C++20.

In addition, even the C standards committee was in favour of it for its language.

So in the light of this, if even C and C++ plan to change their design (and they've always supported a wide range of architectures even exotic ones), I think it would be a great idea for zig.

@andrewrk andrewrk added accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Dec 31, 2020
@andrewrk andrewrk modified the milestones: 1.0.0, 0.8.0 Dec 31, 2020
@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 31, 2020

The reasoning here is similar to our choice to have explicitly sized integers instead of platform-dependent bit widths. Zig chooses correctness over performance here. This decision means that code that uses bitwise operators or @clz/@ctz is portable. It is also consistent with the decision on floats, to standardize IEEE754 behavor. This decision does not preclude a language extension or library adding ones complement or other integer or floating point types, but the uXX and iXX types are guaranteed to have twos complement representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants