lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Lua list <lua@bazar2.conectiva.com.br> writes:
>
>That you get
 
>to/have to choose the type of number
>
>is fine. That there is only once
 
>kind of number and it
>
>is assumed not to support bitwise
 
>operations is
 
>not so fine.

Does the type that supports bitwise ops actually have to be the number
type at all? Perhaps the solution is to extend the string type to allow it
to behave as a non-textual sequence of bytes. There could be
functions/operators to refer to specific ranges of bytes in the string and
then perform various ops on them (arithmetic, bitwise, etc) as if they
were integer words.

For example, strings could have a method called "excerpt" that returns an
object referring to a range of bytes in the string:-

	str = " <some binary data, perhaps from a file> "
	exc1 = str.excerpt(5, 4)
	-- Refers to a sequence of length 4 starting at index 5.

	exc2 = str.excerpt(9, 4)
	-- Refers to a sequence of length 4 starting at index 9.

This object would then have lots of useful methods for bitwise ops and
other things:-

	anotherStr = exc1.bitAnd(exc2)

...or, if the operators are added to the language

	anotherStr = exc1 & exc2

...and so on.

We would need integer routines that could handle any integers of any
length and the excerpt object would need options for endianity, etc. Note
that you could store several excerpts in a table so you could have a
convenient struct-like thing to hold several fields of a single string:-

	structoid = {
			x = str.excerpt(1, 4),
			y = str.excerpt(5, 4),
			z = str.excerpt(9, 4)
		}
	
	foo = structoid.x & structoid.y	

I think the advantages of this approach are that it is easily extended and
it doesn't force the user to choose between floating point numerics and
useful bitwise ops.

Just a thought...
&.



#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared 
by NetIQ MailMarshal.
The Blackpool Sixth Form College.
#####################################################################################