From 2b29424efd05fb059ba35f16eb804613676c869d Mon Sep 17 00:00:00 2001
From: praschke
+ To truncate the significant bits of a number out of range of the destination type, use {#link|@truncate#}. +
If {#syntax#}T{#endsyntax#} is {#syntax#}comptime_int{#endsyntax#}, then this is semantically equivalent to {#link|Type Coercion#}. @@ -9380,17 +9390,11 @@ fn List(comptime T: type) type { or same-sized integer type.
- The following produces safety-checked {#link|Undefined Behavior#}: + This function always truncates the significant bits of the integer, regardless + of endianness on the target platform.
- {#code_begin|test_err|cast truncated bits#} -test "integer cast panic" { - var a: u16 = 0xabcd; - var b: u8 = @intCast(u8, a); - _ = b; -} - {#code_end#}- However this is well defined and working code: + Calling {#syntax#}@truncate{#endsyntax#} on a number out of range of the destination type is well defined and working code:
{#code_begin|test|truncate#} const std = @import("std"); @@ -9403,8 +9407,7 @@ test "integer truncation" { } {#code_end#}- This function always truncates the significant bits of the integer, regardless - of endianness on the target platform. + Use {#link|@intCast#} to convert numbers guaranteed to fit the destination type.
{#header_close#}