---
title: "insertBits / bitfieldInsert: 셰이더 함수 사용법, 계산 예제와 입력 범위·주의점"
description: "정수의 연속된 비트 구간을 다른 정수의 낮은 비트로 바꿉니다. 이웃 플래그를 유지하며 비트 필드를 갱신할 때 사용합니다. 선택한 구간만 바뀝니다. 삽입 구간보다 높은 비트는 버립니다. 시작 위치와 길이는 비트 폭의 제한을 받습니다."
url: https://typeshade.dev/ko/reference/functions/insertBits/
language: ko
---

# `insertBits`

함수

정수의 연속된 비트 구간을 다른 정수의 낮은 비트로 바꿉니다.

**다른 언어의 관련 이름**

관련 연산은 인수, 지원 범위와 의미가 다를 수 있습니다. 다른 언어의 이름을 TypeShade 별칭으로 사용할 수는 없습니다. 지원하는 형태와 제한은 대상 언어에 따라 다릅니다.

- GLSL: `bitfieldInsert`

[대상별 출력](https://typeshade.dev/ko/reference/functions/insertBits/#targets)

## 예제 비교

선택한 구간은 새 값의 낮은 비트로 바뀝니다. 구간 밖의 기존 비트는 유지됩니다.

**모델의 전제**`e: u32; offset = 4; count = 4; newbits = 3`

**예제 1`insertBits(0, 3, 4, 4)`**

**결과**

`48 = 0x00000030`

정수의 이진 표현: `0`

**예제 2`insertBits(16, 3, 4, 4)`**

**결과**

`48 = 0x00000030`

정수의 이진 표현: `16`

**예제 3`insertBits(181, 3, 4, 4)`**

**결과**

`53 = 0x00000035`

정수의 이진 표현: `181`

예제는 설명용 계산 모델이며 GPU를 실행하지 않습니다. 정확한 호출 형식은 아래 컴파일러 참고 자료에서 설명합니다. 소수 결과는 표시할 때 반올림했습니다. 각 예제는 명시한 전제를 따릅니다. 실제 셰이더의 결과는 대상 언어의 정밀도와 실행 규칙에 따릅니다.

## 활용

이웃 플래그를 유지하며 비트 필드를 갱신할 때 사용합니다.

## 입력 범위와 주의사항

선택한 구간만 바뀝니다. 삽입 구간보다 높은 비트는 버립니다. 시작 위치와 길이는 비트 폭의 제한을 받습니다.

셰이더의 입력 규칙과 정밀도는 [WGSL](https://www.w3.org/TR/WGSL/)과 [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf)에서 규정합니다.

## 관련 함수

- [`countOneBits`](https://typeshade.dev/ko/reference/functions/countOneBits/)
- [`reverseBits`](https://typeshade.dev/ko/reference/functions/reverseBits/)
- [`countLeadingZeros`](https://typeshade.dev/ko/reference/functions/countLeadingZeros/)
- [`countTrailingZeros`](https://typeshade.dev/ko/reference/functions/countTrailingZeros/)

**WGSL**

`insertBits(a, b, c, d)`

**GLSL ES 3.00**

`_ibits(a, b, c, d)`

시그니처와 이름 아래 문장, 컴파일러 메시지는 커밋 af54ddad5의 컴파일러가 쓴 영어 원문입니다.

Inserts the low `count` bits of `newbits` into `e` at bit `offset`; the offset and count are clamped to the 32 bits as WGSL specifies.

## 구문

```ts
declare function insertBits<V extends vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: V, a2: u32, a3: u32): V
declare function insertBits<P0 extends number, P1 extends number>(a0: P0, a1: P1, a2: u32, a3: u32): ScalarOf<P0, P1>
declare function insertBits<T extends Numeric>(e: T, newbits: T, offset: number, count: number): T
```

매개변수나 반환 위치에 쓰인 `number`는 스칼라가 들어갈 자리입니다. 앰비언트 파일은 GPU 스칼라를 브랜드가 붙은 number로 선언하고 그 브랜드는 선택이라서, 리터럴도 브랜드가 붙은 스칼라도 이 자리에 들어갑니다.

`type f32 = number & { readonly [f32Tag]?: true }`

타입으로 쓴 `number`는 쓰이는 곳을 보고 추론하며, `f32`, `i32`, `u32`는 너비를 고정합니다. 그 행과 컴파일러가 낸 결과는 [TypeScript에서](https://typeshade.dev/ko/guide/language/from-typescript/) 페이지에 있습니다.

시그니처에는 앰비언트 파일이 스스로 쓰려고 선언한 타입이 나올 수 있습니다. 아래가 그 선언입니다.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## 매개변수

- `e` `V``P0``T`
- `newbits` `V``P1``T`
- `offset` `u32``number`
- `count` `u32``number`

## 반환값

`V``ScalarOf<P0, P1>``T`

## 설명

A `_ibits` helper on GLSL ES 3.00.

## 대상별 출력

**WGSL**

`insertBits(a, b, c, d)`

**GLSL ES 3.00**

`_ibits(a, b, c, d)`

## 예제

- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/ko/guide/examples/bit-bump/)

## 함께 보기

- [함수](https://typeshade.dev/ko/reference/functions/)
- [내장 함수 표](https://typeshade.dev/ko/guide/language/builtins/#bits)
