<div dir="ltr"><div dir="ltr"><div>On Thu, Dec 9, 2021 at 8:04 AM Java House <<a href="mailto:java4dev@gmail.com">java4dev@gmail.com</a>> wrote:<br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello<div><br></div><div>I am trying to build a binary number but I cannot find the correct way:</div><div><font face="monospace">13> <<1:1/bitstring, 0:1/bitstring,0:1/bitstring,0:1/bitstring,0:1/bitstring,0:1/bitstring,0:1/bitstring,0:1/bitstring>>.<br>** exception error: bad argument<br>     in function  eval_bits:eval_exp_field1/6 (eval_bits.erl, line 123)<br>     in call from eval_bits:create_binary/2 (eval_bits.erl, line 81)<br>     in call from eval_bits:expr_grp/4 (eval_bits.erl, line 72)</font><br></div><div><br></div><div>How do we build a binary number e.g.</div><div>10000000 or 10101010 or even a binary number that is not just 8 bit but 12, 14 or 17 bit long</div></div></blockquote><div><br></div><div><div class="gmail_quote">I think the confusion here is that there is a type called binary for dealing with bits/bytes/words and there's also the binary number system (base 2). You don't need the binary type or binary syntax to work with integers in base 2.</div><br><div class="gmail_quote"></div></div><div dir="ltr"><div>To write an integer literal in radix 2 (binary):</div><div><br></div><div><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">> 4095 =:= 2#111111111111.</span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">true</span></p></div><div><br></div><div>You can also use that syntax (or any other way to represent an integer) when constructing a binary (note the bit length of 12).</div><div><br></div><div>> <span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-family:Menlo;font-size:11px"><<2#111111111111:12>>.</span></div><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><<255,15:4>></span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br></p><div>If, for some reason, you wanted to construct it a bit at a time:</div><div><br></div><div><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">> <<1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:1>> =:= <<2#111111111111:12>>.</span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">true</span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal">The /bitstring modifier is not really relevant to this, it is for constructing or destructing a larger binary from other binaries that may not have a multiple-of-eight length.</p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal"><br></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal"><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">> </span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-family:Menlo;font-size:11px"><<<<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring, <<1:1>>/bitstring>> =:= <<2#111111111111:12>>.</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">.</span></p></div></div><div class="gmail_quote"><br></div><div class="gmail_quote">To display integers in base 2, you could use integer_to_list/2 or integer_to_binary/2 or a format string.</div><div class="gmail_quote"><br></div><div class="gmail_quote">





<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">> integer_to_list(4095, 2).</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">"111111111111"</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">> integer_to_binary(4095, 2).</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures"><<"111111111111">></span></p><p class="gmail-p1" style="margin:0px;font:11px Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">> io_lib:format("~.2B", [4095]).<span class="gmail-Apple-converted-space"> </span></span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">






</span></p><p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures">"111111111111"</span></p></div><div class="gmail_quote"><br></div><div class="gmail_quote">-bob<br></div><div> </div></div></div>