@shlWithOverflow

@shlWithOverflow

도입 @shlWithOverflow는 왼쪽 시프트(shift)를 수행하면서 결과에 오버플로우 비트까지 함께 돌려주는 내장 함수(Builtin)예요. 시프트할 때 비트가 밀려나오는지(오버플로우)까지 한 번에 알고 싶을 때 써요.

출처: Zig Documentation

본문

@shlWithOverflow(a: anytype, shift_amt: Log2T) struct { @TypeOf(a), u1 }

a << b 연산을 수행하고, 그 결과와 오버플로우 가능 여부를 나타내는 비트를 튜플(tuple)로 돌려줘요.

shift_amt의 타입은 log2(@typeInfo(@TypeOf(a)).int.bits) 비트를 가진 부호 없는 정수예요. shift_amt >= @typeInfo(@TypeOf(a)).int.bits인 경우 안전 검사(safety-checked) Illegal Behavior가 발동되기 때문이에요.

See also:

더 알아보기 (Learn more)

  • 시프트한 비트가 타입 비트 폭을 넘어 밀려나오는지까지 확인하고 싶다면 @shlWithOverflow를 쓰면 돼요.
  • 오버플로우 검사 없이 그냥 시프트만 하고 싶다면 @shlExact를 써요.
  • 오른쪽 시프트 버전이 필요하다면 @shrExact도 확인해 보세요.