Skip to content

Bitwise AND between c_short and hex value 0x8000 causes range error due to unexpected interpretation of the most-significant bit #22716

Open
@rullo24

Description

@rullo24

Zig Version

0.14.0-dev.850+ddcb7b1c1

Steps to Reproduce and Observed Behavior

When returning the bitwise AND between a c_short and a hex value, the most-significant bit is considered out-of-range, despite still being within the bounds of a c_short (u16). As seen in the error message below, this is because I presume the 0x8000 is being converted to the i16 equivalent and then being pushed through bitwise operations (-32767 <= i16 <= 32767).

For example (where _getCurrKeyState(virt_key) returns a c_short):

// checking if a key is currently activated
pub fn isPressed(virt_key: VK) bool {
    return ((_getCurrKeyState(virt_key) & 0x8000) != 0); // key pressed (down)
}

Error Message:

PS F:\Coding\01.Projects\06.Zig\01-Zeys\example\zig-out\bin> zig build
install
└─ install key_press
   └─ zig build-exe key_press Debug native 1 errors
F:\Coding\01.Projects\06.Zig\01-Zeys\src\zeys.zig:78:43: error: type 'c_short' cannot represent integer value '32768'
    return ((_getCurrKeyState(virt_key) & 0x8000) != 0); // key pressed (down)
                                          ^~~~~~
referenced by:
    waitUntilKeyPressed: F:\Coding\01.Projects\06.Zig\01-Zeys\src\zeys.zig:71:21
    main: F:\Coding\01.Projects\06.Zig\01-Zeys\example\src\key_press.zig:13:29
    WinStartup: C:\Program Files\zig\lib\std\start.zig:616:37
    comptime_0: C:\Program Files\zig\lib\std\start.zig:65:21
error: the following command failed with 1 compilation errors:
C:\Program Files\zig\zig.exe build-exe -freference-trace=10 -luser32 -ODebug --dep zeys -Mroot=F:\Coding\01.Projects\06.Zig\01-Zeys\example\src\key_press.zig -Mzeys=F:\Coding\01.Projects\06.Zig\01-Zeys\src\zeys.zig --cache-dir F:\Coding\01.Projects\06.Zig\01-Zeys\example\.zig-cache --global-cache-dir C:\Users\conno\AppData\Local\zig --name key_press --zig-lib-dir C:\Program Files\zig\lib\ --listen=-
Build Summary: 2/5 steps succeeded; 1 failed
install transitive failure
└─ install key_press transitive failure
   └─ zig build-exe key_press Debug native 1 errors
error: the following build command failed with exit code 1:
F:\Coding\01.Projects\06.Zig\01-Zeys\example\.zig-cache\o\41affe15a6c2524836c8c2bacba0da2e\build.exe C:\Program Files\zig\zig.exe C:\Program Files\zig\lib F:\Coding\01.Projects\06.Zig\01-Zeys\example F:\Coding\01.Projects\06.Zig\01-Zeys\example\.zig-cache C:\Users\conno\AppData\Local\zig --seed 0x3d38602b -Z355eda3502fcbc06

Expected Behavior

The most-significant bit of a c_short should still be considered within range when bitwise AND operations are taken against it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions