![]() |
|
Changing data within bytes... - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Changing data within bytes... (/showthread.php?tid=13632) |
Changing data within bytes... - Nikkey - 2009-07-17 Tends to sometimes be rather hard when it shouldn't be. You may be able to note this especially well when working with bits and strings, where you have to modify characters yourself. Or if you're a LISPer and is really annoyed by the fact that there's no general function for any sequence (Though there is, elt, but it's slower compared to other specialized functions). So yeah, I got bored and modified some setf-functions and made a macro character which works like array-refering or hash-refering in python.
Spoiler
Simply saying, one can refer to any sequence or array, including characters and integers by this #[]-macro: Code: > #[3 1] ; Second bit in the integer 3Can't make things like this in C, python, perl or anything else, eh? ![]() I'm working on making it able to make dispatched arrays and such, so you can modify strings, arrays and whatever else destructively. Kinda like Python's [1:2:3] way of dealing with arrays, just through '(1 2 3) instead. A destructive string-xoring in lisp, through one change in the #[-macro and adding in a (setf logxor)-function would be like this: Code: (defun string-xor (string1 string2)Which looks way prettier and easier to understand than your C or python string-xoring
Spoiler
Changing data within bytes... - Fiel - 2009-07-17 Python does not allow strings or chars to be xor'd. You have to change them from strings to bytes, xor them, then convert back. Code: >>> MyChar = "P"Changing data within bytes... - Nikkey - 2009-07-17 Fiel Wrote:Python does not allow strings or chars to be xor'd. You have to change them from strings to bytes, xor them, then convert back. I don't know any language which allows the simple str1 ^ str2-feat, nor the char ^ int, but in python it would be something like Code: import structNow, that would require the same size on str1 and str2, but repeating str2 would be rather easy to produce, I'd assume. I could eventually modify logxor in lisp so it would work as the non-destructive-string-xor-function, but it would require stuff I've not worked with as of right now. Besides, there's really no need for it in Lisp, unless you send in data to the xor-function which you don't know whether's a number or a string. But then again, you could just check its type. Changing data within bytes... - AngelSL - 2009-07-17 Umm.. what?! Well I don't do LISP or Python so.. Changing data within bytes... - Nikkey - 2009-07-17 AngelSL Wrote:Umm.. what?! In lisp you can make your own macros and symbol-macros, which can ease your work by a lot. For example, lisp does not have for-loops or while-loops (and interestingly enough, I've never had the need to make a for-macro either). You can, however, make a while loop by simply writing this: Code: (defmacro while (test &body body)(This is the easiest macro I know of really, I don't know if I'm able to make an easier.) The lisp macros I defined above makes me able to avoid long expressions which regards arrays, bits, characters and hash-tables, and focus on the main work instead. For example, if I have a hash-table, which has a key which points towards an array, and that array is filled with strings, and I want to either change a character or a string, I will be able to do so easily through this macro (The expression would be equal to #[ht key strnum charnum], instead of the long and big (char (aref (gethash key ht) strnum) charnum).) Changing data within bytes... - Spaz - 2009-07-17 Lisp: where you have to modify the language to get anything useful done. I don't really get what's going on here. You made a macro/function that lets you take the nth element of any sequence? Doesn't sound that special to me. :-\ Changing bits isn't hard if you write a function for it. In C++:
Spoiler
Edit: I wrote that code a while ago. A non-template function taking a void* would result in smaller code. Changing data within bytes... - Dusk - 2009-07-17 Devil's Sunrise Wrote:Can't make things like this in C, python, perl or anything else, eh? I think you can in C *scratches head* In py/pl you can do something to the same purpose without writing a whole crapload of code. You wouldn't usually use Perl for the same things you'd want Lisp for anyway.
whee
Changing data within bytes... - Kortestanov - 2009-07-17 If you just want a quick and simple way to xor every character in a string without doing too many convertions, just make a dll in asm and make an external dll call. This way you will also gain some performance, if that matters to you. Changing data within bytes... - Nikkey - 2009-07-17 Spaz Wrote:Lisp: where you have to modify the language to get anything useful done. Thing is, it's easier to refer to the bit through this, correct? Code: (un)signed whatever n = x;Dusk Wrote:I think you can in C *scratches head* In py/pl you can do something to the same purpose without writing a whole crapload of code. You wouldn't usually use Perl for the same things you'd want Lisp for anyway. Oh, that's really fancy. I don't know what you'd use Perl especially for, though I know you guys have a Memoization module:
Spoiler
If you're able to write modules which work like this, then it's amazing. In LISP, you could always make a macro which do the same thing:
Spoiler
Though this would be rather slow for some cases (like where you can use an n-dimensional array instead), and possibly more inefficient unless you optimise the code some. Kortestanov Wrote:If you just want a quick and simple way to xor every character in a string without doing too many convertions, just make a dll in asm and make an external dll call. This way you will also gain some performance, if that matters to you. I suppose you could, and I suppose it wouldn't be too hard/ugly to implement something like that. But that's not really the idea behind this function. The idea was just to show lisp's flexibility, and hopefully something we would be able to do later on. I'm aware that changing bits (and bytes) in programming-languages doesn't occur extremely frequently, but since there's nothing to lose by having this function available, then why not implement it to ease the work for programmers? Changing data within bytes... - Spaz - 2009-07-17 Devil's Sunrise Wrote:Thing is, it's easier to refer to the bit through this, correct?No, it's not any easier than the below code. In fact, I think the code below is more explicit about what it's doing and thus easier to read. Code: (un)signed whatever n = x;Changing data within bytes... - Fiel - 2009-07-17 Do these programming languages also work on big endian processors? That could make all of this wrong. Changing data within bytes... - Nikkey - 2009-07-17 Spaz Wrote:No, it's not any easier than the below code. In fact, I think the code below is more explicit about what it's doing and thus easier to read. Well, I suppose that's based on preference. For example, a Java/C/C++ programmer would most likely scream in pain if he's told to actually write the following: Code: int array = makeArray(x);Code: int array[x];But you can't deny that referring to n[y] is shorter than getbit(n, y), and that referring to n[y] when defined within a compiler could be so efficient that it would point directly to that bit in the memory. (getbit and setbit, if defined within a compiler, could do the same thing of course) Fiel Wrote:Do these programming languages also work on big endian processors? That could make all of this wrong. I've never thought about that issue, really. That should however be the compiler's, and not the programmers' issue. A piece of uncompiled code shouldn't have to be modified in order to work on another computing system. That should the compilers take care of. |