Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:12:06+00:00 2026-06-04T20:12:06+00:00

In the Erlang shell, I can do the following: A = 300. 300 <<A:32>>.

  • 0

In the Erlang shell, I can do the following:

A = 300.
    300
<<A:32>>.
    <<0, 0, 1, 44>>

But when I try the following:

B = term_to_binary({300}).
    <<131,104,1,98,0,0,1,44>>
<<B:32>>
    ** exception error: bad argument
<<B:64>>
    ** exception error: bad argument

In the first case, I’m taking an integer and using the bitstring syntax to put it into a 32-bit field. That works as expected. In the second case, I’m using the term_to_binary BIF to turn the tuple into a binary, from which I attempt to unpack certain bits using the bitstring syntax. Why does the first example work, but the second example fail? It seems like they’re both doing very similar things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:12:08+00:00Added an answer on June 4, 2026 at 8:12 pm

    The difference between in a binary and a bitstring is that the length of a binary is evenly divisible by 8, i.e. it contains no ‘partial’ bytes; a bitstring has no such restriction.

    This difference is not your problem here.

    The problem you’re facing is that your syntax is wrong. If you would like to extract the first 32 bits from the binary, you need to write a complete matching statement – something like this:

    <<B1:32, _/binary>> = B.
    

    Note that the /binary is important, as it will match the remnant of the binary regardless of its length. If omitted, the matched length defaults to 8 (i.e. one byte).

    You can read more about binaries and working with them in the Erlang Reference Manual’s section on bit syntax.

    EDIT

    To your comment, <<A:32>> isn’t just for integers, it’s for values. Per the link I gave, the bit syntax allows you to specify many aspects of binary matching, including data types of bound variables – while the default type is integer, you can also say float or binary (among others). The :32 part indicates that 32 bits are required for a match – that may or may not be meaningful depending on your data type, but that doesn’t mean it’s only valid for integers. You could, for example, say <<Bits:10/bitstring>> to describe a 10-bit bitstring. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Erlang interactive shell you can bind variables to values. If I would
I'm trying to write a recursive fun in an Erlang shell, but I keep
I made in erlang shell: 1> node(). nonode@nohost But 2> net_adm:ping(node()). pang Why? What's
At the Erlang shell: > orddict:fetch(b, [{d, 2}, {a, 1}, {b,5}, {c,9}]). ** exception
I typed erl to start the erlang shell (Eshell V5.7.4). The shell starts. But,
Is it possbile to clear erlang shell current output for next time's try and
I am using a single module a lot in the Erlang shell. Is there
In Erlang is there any way that a message sender can wait on a
The Erlang documentation states the following about gen_servers: ... Note that for any other
Three erlang processes have been created in erlang shell and registered locally, named Pid,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.