Skip to main content

L1331 - Truncated Integer in Binary Segment

Warning

foo() ->
<<256>>.
%% ^^^ warning: integer value outside range of binary segment will be truncated

Explanation

An integer literal in a binary segment must fit the segment's bit size and sign, otherwise it is silently truncated to the segment width, producing a different value than the one written in the source code.

Examples of out-of-range segments:

<<256>> %% default size is 8 bits (0-255); 256 wraps to 0
<<150:7>> %% 7 unsigned bits hold 0-127; 150 is truncated
<<-130:8/signed>> %% 8 signed bits hold -128..127; -130 is truncated

Fix the value or the segment size/sign so the value fits:

<<256:16>> %% 16 bits can hold 256
<<150:8>> %% 8 bits can hold 150
<<-130:16/signed>>

This warning can be suppressed with -compile(nowarn_truncated_integer).