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 7811189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:04:10+00:00 2026-06-02T04:04:10+00:00

I have a problem with matching a variable number of bits in a binary,

  • 0

I have a problem with matching a variable number of bits in a binary, which I narrowed down to the following toy example to extract the first few bits of a binary:

f(<<H:Bits, _/binary>>, Bits) ->
    H.

The compiler (R15B01) tells me variable 'Bits' is unbound. On the other hand, this works fine:

f(X, Bits) ->
    <<H:Bits, _/binary>> = X,
    H.

And it does what I expect, for instance f(<<1,2,3,4>>, 8) yields 1, and f(<<1,2,3,4>>, 16) yields 258.

Why doesn’t the first form work? Shouldn’t it be equivalent?

  • 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-02T04:04:11+00:00Added an answer on June 2, 2026 at 4:04 am

    In your first example, f(<<H:Bits, _/binary>>, Bits) -> H, the variable Bits is not previously bound – it occurs both the first and the second argument pattern, but they are not matched in any specific order, so Bits within the binary is considered to be unbound. In the second example, f(X, Bits) -> <<H:Bits, _/binary>> = X, Bits becomes bound before the matching of <<...>> = X begins.

    The first example could be read as “take the number Bits given in the second argument and extract that many bits from the binary as H” (what you wanted), but could also be read as “Grab some arbitrary number Bits of bits (perhaps as many as possible) from the binary as H, and then check that it happens to be the same number as the one passed in the second argument”. To avoid this ambiguity, the compiler rejects the program.

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

Sidebar

Related Questions

I have the following matching problem: I have two data.frames, one with an observation
I have a problem which I can't seem to be figuring out: I load
I have a problem with matching text in Vim buffers. I have a specific
I have come across a problem of matching a string in an OCR recognized
I'm matching identifiers, but now I have a problem: my identifiers are allowed to
I have the following problem: Given a Guice type literal TypeLiteral<T> template and a
I'm having perl regex matching problems with a network script I have, I've managed
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -

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.