Movatterモバイル変換


[0]ホーム

URL:


  1. Glossary
  2. Bitwise flags

Bitwise flags

Bitwise flags are sets of variables, usually simple number values, which can be used to enable or disable specific usages or features of a method or other code structure. They can do this quickly and efficiently because they operate at the bit level. Related flags in the same group are generally given complementary values representing different bit positions in a single value (e.g., hexadecimal), so that multiple flag settings can be represented by a single value.

For example, in theWebGPU API, aGPUBuffer object instance is created using theGPUDevice.createBuffer() method. When invoking this method, you define ausage property in the descriptor containing one or more flags that enable different allowed usages of that buffer.

js
const descriptor = {  usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.MAP_WRITE,};

These values are defined inside the same namespace, and each one has a hexadecimal value:

Usage flagHexadecimal representationDecimal equivalent
GPUBufferUsage.MAP_READ0x00011
GPUBufferUsage.MAP_WRITE0x00022
GPUBufferUsage.COPY_SRC0x00044
GPUBufferUsage.COPY_DST0x00088
GPUBufferUsage.INDEX0x001016
GPUBufferUsage.VERTEX0x002032
GPUBufferUsage.UNIFORM0x004064
GPUBufferUsage.STORAGE0x0080128
GPUBufferUsage.INDIRECT0x0100256
GPUBufferUsage.QUERY_RESOLVE0x0200512

When you query theGPUBuffer.usage property, you get a single decimal number returned, which is the sum of the different decimal values for the different usage flags. Returning to the above example, queryingGPUBuffer.usage for theGPUBuffer created with the usage specified earlier would return the following:

  • GPUBufferUsage.COPY_SRC's decimal equivalent, 4
  • AddGPUBufferUsage.MAP_WRITE's decimal equivalent, 2
  • Equals 6.

Because of the values chosen for the different flags, each combination of values is unique, so the program can tell at a glance which flags are set from a single value. In addition, you can easily test which flags are set in the combined value using the bitwise and operator:

js
if (buffer.usage & GPUBufferUsage.MAP_WRITE) {  // Buffer has MAP_WRITE usage}

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp