|
|
| Author |
Message |
< Erlang ~ bnot and Bit String Comprehension |
| sexmith |
Posted: Mon Jan 26, 2009 4:11 am |
|
|
|
Joined: 03 Jan 2008
Posts: 5
Location: Ohio USA
|
I am trying to use bnot within a Bit String Comprehension; but my logic fails every time.
First I am assuming that a bnot on 2#101 would yield
2#010 or 2. No it yields a -6. I am assuming the default is unsigned; yet is this a sign issue or most likely I have no clue as to what bnot does.
bxor works as expected. 2#101 bxor 2#111 yield 2#010.
bxor works as expected within Bit String Comprehension
see below:
io:format("~3.2.0B ~3.2.0B ~3.2.0B ~3.2.0B ~3.2.0B~n",[A,B,C,D,E] = binary_to_list(<< << (2#111 bxor Y) >> || <<Y:3>> <= <<0:3,1:3,2:3,3:3,4:3>> >>)).
111 110 101 100 011
ok
What am I missing about bnot.
R Sexmith |
|
|
| Back to top |
|
| mhenoch |
Posted: Mon Jan 26, 2009 11:10 am |
|
|
|
User
Joined: 06 Nov 2008
Posts: 29
|
There is no difference between 2#101 and 2#00000101 (and so on), so bnot can't know which part of the number you want to invert. Try: Code: bnot 2#101 band 2#111
bnot has higher precedence than band, so the example works without parentheses. |
|
|
| Back to top |
|
| sexmith |
Posted: Tue Jan 27, 2009 1:52 am |
|
|
|
Joined: 03 Jan 2008
Posts: 5
Location: Ohio USA
|
What I hear your say is:
Erlang is a 8bit/byte machine. All bitwise operators (band bxor bor bnot bsl bsr) operate on 8bits.
Erlang's definition of a bit string is: an area of untyped memory which Erlang uses to store bit strings.
[my guess: in this memory area the pointer/offset is
at the bit level].
Ok in my example I capture 3 bits via Y:3.
Y:3 becomes Y for computing. So when Y:3 becomes Y it becomes a padded 8bits. The bitwise operator bxor worked for me because 2#111 becomes padded 8bits 00000111 coupled with the nature of xor where the Zeros do not affect the result. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|