Guest User

Untitled

a guest
Nov 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. % because 256 needs 9 bits to represent it
  2. % although it is 2^8
  3. 62> <<256>>.
  4. <<0>>
  5. % because the segments of an Erlang binary
  6. % has to be divisible by 8. Hence
  7. % 1000 0000, 0
  8. 57> <<256:9>>.
  9. <<128,0:1>>
  10.  
  11. 66> <<1024:10>>.
  12. <<0,0:2>>
  13. 69> <<1024:11>>.
  14. <<128,0:3>>
  15.  
  16. % <<2017>> gets truncated to 8 bits automatically
  17. 82> f(), <<A:8,B/binary>> = <<2017>>.
  18. <<"á">>
  19. 83> B.
  20. <<>>
  21. 84> A.
  22. 225
  23. % but if you specify the bitnumber to represent it,
  24. % there is no loss, although in this case it cannot be
  25. % matched with /binary because it is not divisible by 8
  26. 85> f(), <<A:8,B/binary>> = <<2017:12>>.
  27. ** exception error: no match of right hand side value <<126,1:4>>
  28. 86> f(), <<A:8,B/bits>> = <<2017:12>>.
  29. <<126,1:4>>
  30. 87> B.
  31. <<1:4>>
  32. 88> A.
  33. 126
  34. % <<128:8>>
  35. % 10, 000000
  36. % << 2, 0:6>>
  37. 101> f(), <<A:2,B/bits>> = <<128>>.
  38. <<128>>
  39. 102> A.
  40. 2
  41. 105> B.
  42. <<0:6>>
  43.  
  44. %-module(binaries).
  45. %-compile(export_all).
  46.  
  47. %bitlist(Bin) ->
  48. % bitlist(Bin,[]).
  49. %
  50. %bitlist(<<>>,Acc) ->
  51. % lists:reverse(Acc);
  52. %bitlist(<<B:1,Rest/bits>>,Acc) ->
  53. % bitlist(Rest, [B|Acc]).
  54. 120> binaries:bitlist(<<8226>>).
  55. [0,0,1,0,0,0,1,0]
  56. 121> binaries:bitlist(<<8226:16>>).
  57. [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0]
  58. 122> binaries:bitlist(<<256>>).
  59. [0,0,0,0,0,0,0,0]
  60. 123> binaries:bitlist(<<256:9>>).
  61. [1,0,0,0,0,0,0,0,0]
  62.  
  63. % aaand tha same thing in one line
  64. 131> [B || <<B:1>> <= <<8226:16>>].
  65. [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0]
  66.  
  67.  
  68. 1> io:format("~ts~n",["\x{2022}"]).
  69. ok
  70. 2> io:format("~ts~n",[[8226]]).
  71. ok
Add Comment
Please, Sign In to add comment