lainchan / λ
Logical NOT elegantly
Cat Mother · 2020-11-03 23:13
The answer will contribute to archiving Cicada 3301 properly.
How can I perform logical NOT a file?
For symbolic reasons, I wish to avoid saving a script.
I can almost accomplish the task in Urbit's dojo.
dictionary · 2020-11-03 23:24
For clarity, I mean "reversing each individual bit in a file".
Leto Atreides I · 2020-11-04 00:23
echo '#include <stdio.h>
int main(int argc,char** argv) { while(feof(stdin) == 0) { putchar(0xff ^ getchar()); }}' > tmp.c; gcc -C tmp.c; cat input_file | ./a.out > output_file; rm tmp.c a.out
Agent Smith · 2020-11-04 02:40
Not very elegant, but I appreciate your help.
St. IGNUcius · 2020-11-04 05:32
python version :P :
python -c 'i = open("in_file", "rb"); o = open("out_file", "wb"); o.write("".join(chr(ord(c) ^ 0xff) for c in i.read()))'
Spooky · 2020-11-04 06:25
>>21818
Wouldn't
./a.out Give a permission denied error? Don't you have to include chmod +x to give it permission first?
setq · 2020-11-04 06:54
>>21825
I think it would get made with +x permission but I might be wrong
Tomohisa "Tom" Kanezaki · 2020-11-04 10:10
Why'd an operation requiring no more than one transistor
require more than one command?
Much less more than one argument
given only a single input?
The complexity seems systemically wrong,
only in place to discourage curiosity.
Otto Normalverbraucher · 2020-11-04 13:23
>>21829
all these layers of abstraction have a purpose though. Sure, you could build a machine that negates its input signal, but can you get it to do anything else? Increased complexity is a real downside, but for all its upsides we still build computers like Neumann taught us to. I wouldn't call it systemically wrong, but I agree that some of the complexity sure does stifle creativity.
Mentat · 2020-11-05 18:13
The word "downside" is very dishonest.
That implies interference by some physical law.
It is an anti-feature, hiding basic calculus.
Delilah · 2020-11-05 19:08
>>21829
You're fixating on the wrong thing. The actual negation is incredibly simple. It's at worst one instruction per byte, if you go with the naive implementation, though you can certainly do better.
Your requirement that the data be a file is what is causing the real complexity. This means you have to talk to the kernel, serialize it, buffer it, etc. Even if we're talking about writing kernel code, you're not going to get much more elegant than iterating through the bytes in memory and negating them.
drop · 2020-11-05 19:50
You could get more elegant by designing shell
to foremost do lambda calculus on files, then
prioritize linking functions and arguments.
For example, computers have bytes.
A logic gate does not have a byte.
The OS needs a concept of a byte.
Why does the typist at a shell?
XERXES · 2020-11-05 20:18
>>21829
reading and writing a file from disk does not take only a single transistor, and it's where most of the complexity of this task comes from
Motoko Kusanagi · 2020-11-05 21:13
We want people to learn though.
Not expose those learning fundamentals
to that much complexity..
Lunch · 2020-11-05 21:26
>>21812
what manga is this?
Agent Richard Gill · 2020-11-05 21:34
http://www.vilous.net
Armitage · 2020-11-05 21:42
>>21862
Thanks lain!
Jens Hansen · 2020-11-05 23:19
>>21858
>>21860
assuming you're OP, what are you even asking for? do you want a way to complement a file using tools you already have? write like 10 lines of C code.
do you want recommendations on programming languages that can do this thing succinctly? i'm not aware of any that have binary file operations as part of the standard library, but any language with operator overloading could do it rather easily. python would probably be the easiest, C++ would be the most performant, and haskell might be more in keeping with the "lambda calculus for files" idea you mentioned. you could even come up with a filesystem that lets you operate on files in this way directly. then the complexity is almost entirely hidden from the user. you're still just sweeping complexity under the rug though, which i don't personally consider an elegant way to solve the problem, but that's just an aesthetic preference. if your goal is to make these kinds of operations easy for the programmer, then sure, the "sweep under the rug" approach makes sense.
do you want to spitball about theoretical computer architectures that avoid the "iterate over every byte and negate it" algorithm that is ultimately necessitated by the fundamental nature of the von neumann architecture? that's an interesting discussion, but i certainly didn't get that from your OP.
John Doe · 2020-11-06 01:08
Computers frustrate me. To consume others' programs,
choose activities among what others' decide to make,
if you only want to have fun, shaping your environment,
you have to learn another's art.
You have to let another's art shape how you think.
By the moment you feel able, you've forgotten
more than you remember about your dreams.
We have computers able to simulate any machine,
yet we can't simulate elegant use of early principles.
Oscar Wilde says in preface to the Picture of Dorian Gray
"To reveal art and conceal the artist is art’s aim."
I've gotten so far in the scavenger hunt
without needing to use outside knowledge.
Cicada 3301 teaches through first principles.
The fact no one gets to exclusively learn the first principles,
makes me angry, because of all the lost ambitions.
The code here >>21818 ACTUALLY did help.
I only wanted to give my long overdue opinion.
McCarthy · 2020-11-06 01:12
By inside knowledge, I do mean to include
information gained through keyword searches.
Aristóteles Amadópulus · 2020-11-06 01:19
By "only wanted" I mean "only wanted to in addition".
Systemically, I "only want" to ensure future pilgrims
learn many tools while sacrificing little will.
Boris Grishenko · 2020-11-09 20:51
Ideally the syntax would read:
dd if=input of=output bs=1 count=1 gate=not status=progress
setq · 2020-11-09 20:59
dd if=input if2=input2 of=output skip=1 skip2=0 bs=1 count=1 gate=xor status=progress
Agent Smith · 2020-11-09 23:46
>>21820
Im not the author of the code, but I think you need to give a precise definitions of what do you mean by 'elegant' and 'saving a script' so people can give you more precise answers.
Spooky · 2020-11-10 14:10
No, I appreciate the answer given. Since I know no elegant or literate solution exists,
not to the standard where someone who just learned about logic gates can explore them.
I really hadn't expected any meaningful answer at all,
so I do not take for granted the fact that I got one.
I just don't want any one to feel satisfied with the status quo.
Its really unacceptable that our computers lack tools to enable educational play.
Bobby Newmark · 2020-11-10 23:38
>>22075
How can you assume that there is not an elegant solution without having a precise definition of elegant?
I think this thread can produce very good content, we can probably start with some another assumption like "the file is loaded on memory" or "the file is in a static memory in some FPGA", Idk for real, but at least Im sure you could provide more details.
Arisu Mizuki · 2020-11-11 00:12
>>22084
Elegancy conforms to the computer's particular embedded hardware,
thereby performing the task in as specialized a way as possible
doing minimal molecular work.
So, if technically one could use a single transistor
via a morse code transmitter, an elegant solution
causes the computer to activate as few transistors
and as few circuits as possible for a given case.
Without intimately knowing my hardware,
one can only give an elegant solution
if the linux kernel and unix environment have
an API to standardize exactly just such a solution.
Roy Batty · 2020-11-11 01:34
>>22085
>Elegancy conforms to the computer's particular embedded hardware,
so no program written in a general-purpose OS environment is elegant? Even the notion of "file" is an abstraction over the hardware that does not reflect actual bits in a computer.
Joey · 2020-11-11 02:40
>>22087
Not true,
you can write elegant general-purpose code,
so long as you can assume you know which
API attached to the kernel statistically will
execute the most elegant operations.
Faye Valentine · 2020-11-11 02:53
>>22085
You are totally wrong(if you are using anyone of Intel cpus) no matter which kernel or whatever your think about logical gates, It will need to run a soykafload of firmware code before being able to follow even a single instruction (take a look on Intel FSP).
btw If you are using any other modern CPU the case will not be very different from this, you will be causing a soykafload of things your dont even wrote and probably cant read too (withouth reverse engineering the firmware), just boostrap your hardware..
You are assuming a lot of things wrong, when the kernel take over control of the system, you machine have already executed millions of instructions.
But... assumind an legacy BIOS environment, you will cant escape from nothing of describred before, but you can use BIOS interrupts to load your file and negate all the bytes using asm in 16bit (aka REALMODE)
The Plague · 2020-11-11 03:01
>>22089
Again you missed the point when using elegance to define operations.
Let me know if im wrong, but im thinking you just learned about logical gates few days/months ago and just dont managed to get how all the things fit together in a computer system.
Just let me know what you think the computer(hardware/software) does when you just run a program, you can describe it at any level you want, maybe starting at the kernel loading the program imagem through some syscall... bla bla bla... until "machine" instructions starts to filling CPU, if you can relate the start of this to the gate level we will got something to describe as "elegant"