# Bitwise - Overtly set the 8th bit on a i8: Testing twoscompliment

**URL:** <https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774>\
**Category:** Explain\
**Tags:** language\
**Created:** [August 27, 2024, 1:26am UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774 "2024-08-27T01:26:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![endlessly\_amused](https://ziggit.dev/user_avatar/ziggit.dev/endlessly_amused/32/2551_2.png) [@endlessly\_amused](https://ziggit.dev/u/endlessly_amused)\
**Post date:** [August 27, 2024, 1:26am UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/1 "2024-08-27T01:26:01Z")

</div>

Hi,

What is the correct way of setting the 8th bit to 1 overtly. Irrespective of the data type?  
U8, I8 I don’t care. The 8th bit is to be set.

## Try 1 - Let’s use an or

```zig
var my_i_byte1:i8 = 0;
my_i_byte1 |= 0b10000000;
std.debug.print("My_i_Byte1 is number {d}, \n", .{my_i_byte1} );

```

No, type ‘i8’ cannot represent integer value ‘128’  
Clearly my binary number is being converted to a U8

## Solution - But you have got to be kidding me

```zig
var my_i_byte1:i8 = 1;
my_i_byte1 <<= 7;
my_i_byte1 |= 0b01111111;
std.debug.print("My_i_Byte1 is number {d},\n", .{my_i_byte1} );

```

In this solution I start with a 1. Move it to the 8th position by shifting it to the left 7 times  
and then use it. But there must be a better way.

## Why do I want to do this?

I just wanted to ensure I had twos compliment right in my head. The best way to test this is to set the bits manually and ensure it comes to what you think it does eg 11111111 is -1

I just wondered if there was a better way. ie a “proper” OR, AND, NOT that does as it’s told.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [August 27, 2024, 2:54am UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/2 "2024-08-27T02:54:41Z")

</div>

`@bitCast`: [Documentation - The Zig Programming Language](https://ziglang.org/documentation/master/#bitCast)

---

<div class="post-metadata">

**Author:** ![endlessly\_amused](https://ziggit.dev/user_avatar/ziggit.dev/endlessly_amused/32/2551_2.png) [@endlessly\_amused](https://ziggit.dev/u/endlessly_amused)\
**Post date:** [August 27, 2024, 7:14am UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/3 "2024-08-27T07:14:29Z")

</div>

Thanks. Works a treat.

```zig
const std = @import("std"); // this loads the standard library for zig

pub fn main() !void {

    {
    const my_u8:u8=0b1111_1111; // load the u8 up with 1111_1111
    const my_i8:i8=@bitCast(my_u8); // Copy over to i8
    std.debug.print("Is this -1 ? >{d}\n", .{my_i8} ); // check it's minus 1
    // output
    // Is this -1 ? >-1
    }

} // end of main

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [August 27, 2024, 8:09pm UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/4 "2024-08-27T20:09:37Z")

</div>

May I interest you in an Extremely Overengineered solution?

```zig
fn flipBit(v: anytype, b: comptime_int) @TypeOf(v) {
    const t_info = @typeInfo(@TypeOf(v));
    switch (t_info) {
        .Int => |i_info| {
            if (i_info.bits < b) {
                @compileError("value does not contain that bit");
            }
            if (i_info.signedness == .signed) {
                var u_v: @Type(std.builtin.Type{
                    .Int = .{
                        .bits = i_info.bits,
                        .signedness = .unsigned,
                    },
                }) = @bitCast(v);
                const flipper: @TypeOf(u_v) = 1 << b - 1;
                u_v |= flipper;
                return @bitCast(u_v);
            } else {
                const flipper: @TypeOf(v) = 1 << b - 1;
                return v | flipper;
            }
        },
        else => @compileError("can't flip a bit on a non-integer"),
    }
}

test "flip eighth bit" {
    const flipme: i8 = 5;
    std.debug.print("flipped: {d}\n", .{flipBit(flipme, 8)});
}

```

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [August 27, 2024, 9:32pm UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/5 "2024-08-27T21:32:55Z")

</div>

This is great when you’re being paid per lines of code. lol

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [August 29, 2024, 2:17pm UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/6 "2024-08-29T14:17:34Z")

</div>

But not if you’re being [paid per instruction](https://godbolt.org/z/czWj4G6P6)!

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 29, 2024, 2:27pm UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/7 "2024-08-29T14:27:16Z")

</div>

I 'll be nice and enable the `ReleaseSafe` mode: [Compiler Explorer](https://godbolt.org/z/Tvb8MMf9K) 🙂

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [August 29, 2024, 2:29pm UTC](https://ziggit.dev/t/bitwise-overtly-set-the-8th-bit-on-a-i8-testing-twoscompliment/5774/8 "2024-08-29T14:29:34Z")

</div>

Right? Comptime safety beats runtime safety any day of the week.
