# Correctly cast bool to signed int in translate-c · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/830bc41b1f71d5366037f3944d35618d8ca46e51)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 830bc41b1f71d5366037f3944d35618d8ca46e51

Requested commit: 830bc41b1f71d5366037f3944d35618d8ca46e51

Commit: 830bc41b1f71d5366037f3944d35618d8ca46e51

Tree: c253e0e2c163bf1f221930232aff4297924ed2a4

Author: Evan Haas

Committer: Veikka Tuominen

## Message

```
Correctly cast bool to signed int in translate-c

Previously casting a bool to an int would result in the following Zig code:

    @intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(b))));

This is incorrect if `b` is true, since bitcasting a `u1` with the value 1
to an `i1` will result in the value -1. Instead, generate the following code:

    @as(c_int, @boolToInt(b));

Since @boolToInt returns a `u1`, this is only disallowed if the destination
type is one-bit and signed, which can only happen if it's a bitfield
(currently not supported by translate-c)

```

## Parents

- [9f33984119ee698a9b344ad0df7b498b334e0f6e](https://git.cafe/gitcafe/zig/commit/9f33984119ee698a9b344ad0df7b498b334e0f6e?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/830bc41b1f71d5366037f3944d35618d8ca46e51?format=markdown)
