lua-users home
lua-l archive

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


On Jul 10, 2013, at 2:22 PM, Philipp Janda <siffiejoe@gmx.net> wrote:

> Am 10.07.2013 16:46 schröbte Doug Currie:
>> 
>> On Jul 10, 2013, at 5:14 AM, Philipp Janda <siffiejoe@gmx.net> wrote:
>> 
>>> `^` is only taken for numbers, but IMHO it doesn't make much sense to define bit operations on numbers anyway: "Let's take the number of apples and xor it with the number of oranges, right shift by the number of baskets, and we get: total nonsense"! Usually one does arithmetic _xor_ bit manipulation at any given time[*].
>> 
>> Besides the hash table exception you noted, there are also encryption, digital signatures, and hash (message-digest) applications.
> 
> I know of arithmetic in asymmetric encryption, but those are big integers (more than 64 bits). Other than that: encryption transforms a stream of bytes into another stream of bytes, while cryptographic hashes transform byte streams into bit strings or hex strings. From the outside I cannot see any numbers, so I assume that some arithmetic operations (`+` and `*`, mostly) are performed on some internal state, usually on fixed width unsigned integers with wrap around semantics, i.e. uint64_t, uint32_t, uint8_t and maybe uint16_t (if you decompiled the C source code you would probably find some arithmetic on 128+ bit unsigned "numbers" somewhere).
> Am I wrong?

No, it sounds like we are in agreement… to implement these algorithms some datatype is needed that can be used as unsigned integers with wrap around semantics, and that supports both bitwise and arithmetic operations.

> In C you can do arithmetic on pointers, characters, enumerations, and "booleans". Just because someone does `+` and `*` on something in C code doesn't mean it's (supposed to be) a number.

Well, as a programmer weaned on embedded control in tiny hardware, a number is just one way to interpret a bit pattern. There are a lot of interesting things that can be done with these bit patterns that blur the distinction between numbers and "non-numbers," whatever those are. 

Examples (in C) of things I've found useful in my work:

- isolate the rightmost set bit: x & (-x)

- sign extend a field: ((x ^ N) - N) -- where N is the numeric representation of the sign bit, e.g., 32768 for 16 bits

- integer log base 2: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn

The point is that these algorithms apply both arithmetic and bitwise operators to the data.

e