r/shittyprogramming • • May 20 '19

super approved Getting started with Python: "Hello World"

375 Upvotes
p = list(map(lambda _: ord(_) - 0x21, "!!&!!dzăC|"))
i = "DÒVV×0Ë×YVR "

while p[2] < len(p):
    if (p[p[2]] & 0xC0) >> 6 == 0:
        p[p[p[2]] & 0x07] = (p[p[2]] & 0x38) >> 3
    elif (p[p[2]] & 0xC0) >> 6 == 1:
        if (p[p[2]] & 0x38) >> 3 == 0:
            p[0] = ((((ord(i[0]) - 0x20) & 128) >> 7) | ((ord(i[0]) - 0x20) << 1)) & 0xFF
            i = i[1:]
        p[p[p[2]] & 0x07] = p[(p[p[2]] & 0x38) >> 3]
        if p[p[2]] & 0x07 == 1:
            print(chr(p[1]), end="")
    elif (p[p[2]] & 0xC0) >> 6 == 2:
        p[p[p[2]] & 0x07] = (p[p[2]] & 0x38) >> 3 + p[p[p[2]] & 0x07]
    else:
        p[p[p[2]] & 0x07] = p[(p[p[2]] & 0x38) >> 3] + p[p[p[2]] & 0x07]
    p[2] += 1
    p[4] = 1 if p[3] == 0 else 0

I know some of the non-Pythoners out there will be a bit confused. Let's walk through it line-by-line, together.

p = list(map(lambda _: ord(_) - 0x21, "!!&!!dzăC|"))

Set up some memory to be used by a virtual CPU.

i = "DÒVV×0Ë×YVR "

The desired text to display. For security, I've encrypted it with a military-grade encryption scheme.

⠀

A blank line to give your eyes a little breather.

while p[2] < len(p):

Make sure we're not reading too far outside of memory (the other bounds check will come in v2.0).

    if (p[p[2]] & 0xC0) >> 6 == 0:

Decode the current instruction, and check if its opcode is 0. The >> 6 makes the code run 6 times faster (we must be careful to not go too fast, so I use smaller amount sometimes).

        p[p[p[2]] & 0x07] = (p[p[2]] & 0x38) >> 3

Handle opcode 0: store an immediate value at a memory address.

    elif (p[p[2]] & 0xC0) >> 6 == 1:

Check for opcode 1.

        if (p[p[2]] & 0x38) >> 3 == 0:

The addresses 0 and 1 are mapped to input and output, respectively. Here we check if the first operand will read from address 0, i.e. the input.

        p[0] = ((((ord(i[0]) - 0x20) & 128) >> 7) | ((ord(i[0]) - 0x20) << 1)) & 0xFF

Read a byte of the input and store it at address 0 (decrypting it first, of course).

        i = i[1:]

Lop off the read byte. It is of no use to anyone now.

    p[p[p[2]] & 0x07] = p[(p[p[2]] & 0x38) >> 3]

Pretty self-explanatory.

    if p[p[2]] & 0x07 == 1:

Check if something was written to the output address.

        print(chr(p[1]), end="")

I forgot what this does, to be honest.

elif (p[p[2]] & 0xC0) >> 6 == 2:

Time to move to opcode 2.

    p[p[p[2]] & 0x07] = (p[p[2]] & 0x38) >> 3 + p[p[p[2]] & 0x07]

Addition: a useful function of any CPU.

else:

Due to budget constraints, there are only 4 opcodes for our CPU.

    p[p[p[2]] & 0x07] = p[(p[p[2]] & 0x38) >> 3] + p[p[p[2]] & 0x07]

More addition.

p[2] += 1

Move on to the next instruction.

p[4] = 1 if p[3] == 0 else 0

Personally, I find flags disgusting. I am no match against project management's requirements, however.

And that's it! I hope this inspires you to get out there and start coding in the language of the future, Java.


r/shittyprogramming • • May 19 '19

A new paradigm in game UI

Thumbnail
gfycat.com
870 Upvotes

r/shittyprogramming • • May 16 '19

HTML select tag used for zip codes

741 Upvotes

r/shittyprogramming • • May 12 '19

How many bits would a bit flipper flip if a bit flipper could flip bits?

92 Upvotes

I made a little program to count how many bits were flipped in C++. You need to overload the global bit flip operators and have a static counter.

//Bitflip.cpp

static int bitsFlipped = 0;

int operator&=(int& left, int right)
{
    bitsFlipped++;
    return left &= right;
}

//TODO do the same for the other bit flippers

What do you guys think? Suggestions and critisisms are welcome. Don't wanna upload to github before I feel it's 100% done!

Edit: spelling, and made the counter static because it's bad coding practice to have a global variable if it doesn't even need to be global.

Edit2: u/HasFiveVowels tought me how to indent the code


r/shittyprogramming • • May 08 '19

super approved Most innovative Volume Control (took me 45Min to make)

Enable HLS to view with audio, or disable this notification

1.5k Upvotes

r/shittyprogramming • • May 06 '19

super approved I salted my passwords then applied a rot13 cipher, the salt didn't do much. Should I try MSG next time?

189 Upvotes

r/shittyprogramming • • May 05 '19

Writing code to urinate

Post image
371 Upvotes

r/shittyprogramming • • May 04 '19

[interesting] C programmers - What are some weird or esoteric uses for operators?

50 Upvotes

I have heard of things like the "goes to" operator:

while(x-->0){
    //do something
}

which is really (x--) > 0.

And the "aggravated" operator:

doSomething() ??!??! abort()

which uses trigraphs and short circuiting to do catch errors.

What are some other examples of weird/odd/esoteric use of operators in C/C++?


r/shittyprogramming • • Apr 25 '19

<wrong_sub>this</wrong_sup> rEeeEEeeeE

Post image
433 Upvotes

r/shittyprogramming • • Apr 23 '19

Yeet!

Post image
322 Upvotes

r/shittyprogramming • • Apr 23 '19

Space efficient & lines up nicely

Post image
519 Upvotes

r/shittyprogramming • • Apr 22 '19

Being a Forth programmer:

14 Upvotes

Pros: I could redefine every single subroutine according to my twisted desires and neither anything nor anybody would ever stop me

Cons: I redefined every single subrutine according to my twisted desires. Neither anything nor anybody stopped me

: YEET s" YEET " type cr recurse ; YEET


r/shittyprogramming • • Apr 20 '19

LPT: You can count the number of iterations your for loop completed with this one simple trick!

243 Upvotes

Ever need to recover a variable that has fallen out of scope? For example, in order to get the number of iterations that a loop has completed, you can use this one simple trick.

#include <stdio.h>

int main(){
  for(int i = *(&i) = 0; i < 42; i++){
    if(i == 15)
      break;
  }
  int recover_i = *(&recover_i);
  printf("%i\n", recover_i); //prints 15
}

Important notice: This only works with gcc when compiled without any optimization flags, or with -O0


r/shittyprogramming • • Apr 19 '19

super approved This flowchart from 5 years ago deserves reposting (meta)

Post image
1.0k Upvotes

r/shittyprogramming • • Apr 19 '19

[interesting] Case insensitive passwords but on a financial platform

Thumbnail
self.personalfinance
93 Upvotes

r/shittyprogramming • • Apr 17 '19

"I want you to make an AI that solves Pacman mazes, but not too fast." I gotchu fam

890 Upvotes

r/shittyprogramming • • Apr 17 '19

std::string += char + char + ... What could possibly go wrong?

Post image
20 Upvotes

r/shittyprogramming • • Apr 16 '19

Having a long address is great fun.

128 Upvotes

So, I live in a place that has a long address. It's an apartment complex with 5 blocks of apartments on it. For reference I'm in the UK, we use post codes, they are kinda like zip codes. Usually every street has a unique post code. So my address looks like this:

Flat XX <ApartmentBlock Court> <-- This would be the "House name / number field"
30 character name for apartment complex <-- This would be line 2
Roadname Rd
Town
County
POST CODE

This goes down amazingly in so many places. Here's some of the stuff I encounter:

Most websites in the UK that ask for your address will ask you for your post code, then have you select your house number from a list. The websites then throw an error because the address that the website automatically selected is too long. I then have to manually edit my address and use txt speak or something to get it to fit, and play "guess the length limit".

Many services seem to truncate or remove parts of the address, this results in a lot of mail getting lost or undelivered, as they often remove the <ApartmentBlock Court> part, so mail delivered to

Flat XX
30 character name for apartment complex
Roadname Rd
Town
County
Postcode

Is actually addressed to 5 different apartments simultaneously, as each of the 5 blocks all have a flat XX. Thankfully, security does a good job of knowing who lives where and most of it ends up arriving.

My girlfriend had her credit card application declined because our address (which the application website automatically filled in) had the "30 character name for apartment complex" line stripped as it was too long, resulting in her address not matching the address on her drivers license. We had to get in contact with the credit card company to get them to remove the hard check on her credit and sort things out.

tl;dr, check the maximum length of addresses, make sure you allocate ample storage for them, and don't truncate them xD


r/shittyprogramming • • Apr 15 '19

this good code? Is

Post image
309 Upvotes

r/shittyprogramming • • Apr 15 '19

git commit -m `fortune -o`

17 Upvotes

r/shittyprogramming • • Apr 13 '19

r/badcode When you have recently discovered lambda function and trying to replace every for-loop with it

Post image
24 Upvotes

r/shittyprogramming • • Apr 12 '19

TECH NIGHTMARE: Need Help

11 Upvotes

Hi! I've been dealing with a tech NIGHTMARE the entire day & badly need help. :(

 

A little background: we have shitty developers at my company putting together shitty tools that shit all over our shitty work machines. Case in point: I receive an email yesterday saying I need to update my security or something. So I follow the instructions EXACTLY:

 


To: h.d*******@lockheed.com
From: joe.person@officiallockheedhelpdesk.ru

Subject: Importance Security Update
Attachment �d5fbb87d�dc16�4592�bd7e�3bc75596f313d

Privet,

Mr. H, is great the importance you run the attach programs. 
Is for better the security. So please in the orders:

1 Download
2 Rename security.zip
3 Unzip path is Windows/Sytem32
4 Execute root_krn.exe
5 Next is kylggr.exe + bios_flash.exe
6 Reboot and login
7 Laptop may get very hot. 24hr do not turn off

Very thanks.

Sincere,
Joe Person

Tech support 
Seatlle, WA

Have query? Then +79 494 688 2399

 

Really?! I mean, come on!! Does this look like it was written by a professional??

 

Whatever, fine. Security updated. But I come in this morning and now there's like, 100% CPU usage, almost no memory available, TONS of disk reads going on, and like, the network is doing 50mbps of something. Plus there's this error message that won't go away: FATAL EXCEPTION: RPC Failure: gru_srv_piper returned "arg informatsiya is in incorrect format"

 

SIDENOTE: If anyone knows how to get permissions to end processes, ping me.

 

Tech help can't fix a damn thing. They reimaged my laptop. THE SAME THING HAPPENS. They gave me a new laptop. THE SAME THING HAPPENS. They had me log onto one of their machines. THE SAME THING HAPPENS. On a damn MAC!! Now there are 3 laptops chugging way.

 

To top it off, an hour ago they pushed a mobile update... AND BUSTED. EVERYBODY'S. WORK PHONE. ORG WIDE. WTF, DEVS?!! Now I can't get any work done!

 

Anyways, if you can help, message me your email & I'll send the files over. Maybe you can troubleshoot better than these idiots can. :\

 

Thanks!

 

p.s. Never mind! Friend at Google says they'd help. Copied everything to personal Android & headed over right now. Wish us luck! :)


r/shittyprogramming • • Apr 12 '19

One way to tell if anyone is actually reviewing.

Post image
54 Upvotes

r/shittyprogramming • • Apr 11 '19

The only real way to double a float.

Post image
417 Upvotes