bitcast
A low-level operation that takes the raw binary bit sequence of a value and reinterprets it as another data type of the same total bit-width, without preserving the original numerical value.
Core Advantages
It provides direct access to the underlying binary representation of data, enabling advanced optimizations and algorithms. It allows packing different data types (like an int and a float) into the same variable to save space and enables bitwise operations on floats for high-performance hashing or custom math functions.
Common Uses
Bit-casting an integer ID to a float to pack it into one channel of a vec4
Bit-casting a float to an integer to perform bitmasking, shifting, or other bitwise operations on it
'Scrambling' the binary information of an input value via bitwise operations in procedural noise or hash functions
Implementing fast, approximate math functions that rely on bit manipulation ('bit hacks')
How to adjust
It has no intuitive, linear visual effect; its output often looks like random noise or 'glitch art'. For example, visualizing a smooth 0.0-to-1.0 float gradient after bit-casting it to an integer will not produce a smooth transition, but a chaotic, discontinuous pattern. This reveals the vast difference in the underlying binary representations of different data types.
Code Examples
1// Bit-cast an integer ID to a float for packed storage
2const packedFloat = bitcast( particleID, 'float' );
3
4// ...elsewhere, bit-cast it back to perfectly recover the original integer ID
5const unpackedID = bitcast( packedFloat, 'int' );