Why does binary-size(8) behave differently than size(64) in binary matches?

I’m curious how binary-size(8) and size(64) differ, because I’m getting different results than I’d expect.

Specifically, I’d expect identical results from each, but I might be missing some detail. :slight_smile:

As a quick example:

iex(50)> a = "hello world"
"hello world"
iex(51)> <<h::binary-size(8), _rest::binary>> = a
"hello world"
iex(52)> h
"hello wo"
iex(53)> <<h2::size(64), _rest::binary>> = a       
"hello world"
iex(54)> h2                                 
7522537965568948079

Why is there a discrepancy in the results of these matches, given that size() measures in bits and binary-size() is in bytes, which makes them ostensibly the same?

Thanks!

binary-size(8) means take 8 bytes and interpret as a binary (String). size(64) has a default type of integer. Ie its the same as integer-size(64). Hence the result is interpreted as an integer.

7 Likes

Gotcha! I must have missed the interpretation bit in the docs, I’ll look closer. Thanks!!