4 ms·
One thing that's actually extremely common, however, in C code is using ints for bitmasks. I'm not sure how operators like &|! would be implemented with doubles
by azim 16y ago
One thing that's actually extremely common, however, in C code is using ints for bitmasks. I'm not sure how operators like &|! would be implemented with doubles, but they'd need some fancy software implementation.
- mhansen 16y agoJavascript faces similar problems - it has no int type - all numbers are represented as 64-bit doubles. &|! operations are done by converting the double to an int, performing the operation, then converting back to a double.
- celoyd 16y agoJust to be clear, when you say they are done like that, those operators are in the spec and that’s how they act; it’s not just a standard hack. So there is (for example) a ToInt32 function in the spec but not in the language. This surprised me. (I was playing with cryptography in JS and needed to do lots of bitwise operations on hand-made bigints. It was easier than I expected.)
- mhansen 16y agoCan I see your work somewhere? I've been wondering about crypto in javascript. I checked your profile for an email, but it doesn't show.
- celoyd 16y agoIt was an implementation of Trivium, an extremely simple stream cipher. I was also thinking of doing Salsa20, but I got distracted. The code: http://basecase.org/trivium/trivium.js http://basecase.org/trivium/trivium.js Demo: http://basecase.org/trivium/ http://basecase.org/trivium/ Honestly, I lost interest once I got it working with simple test strings. If you do something cool with it, let me know! Edit: Looking at it again, my code is badly undercommented – sorry. But if you follow along with the Trivium specification (http://www.ecrypt.eu.org/stream/ciphers/trivium/trivium.pdf http://www.ecrypt.eu.org/stream/ciphers/trivium/trivium.pdf), which is a model of clarity, it should all make sense mod renaming of variables etc. The outstanding bug is that it assumes that any sequence of 16-bit values can be taken as a UTF-16 string, which is not true. Will e-mail you some general thoughts when I have a few minutes.
- _delirium 16y agoIn the C backend at least, it appears to cast to an int, perform the bitwise operations, and then cast back. Just trying it out now, the function: int foo(int bar) { return bar & 0x01; } compiles to: clue_real_t _foo(clue_real_t fp, clue_optr_t stack, clue_real_t FLOAT0) { clue_real_t sp; clue_real_t FLOAT1; clue_real_t FLOAT2; sp = 0; sp = fp + sp; FLOAT1 = 1; FLOAT2 = (clue_realint_t) FLOAT0 & (clue_realint_t) FLOAT1; return FLOAT2; }