hugo-site/content/posts/a-funny-bug.md

920 B

title date draft
A Funny Bug 2024-09-01T22:51:05-05:00 false

Well I just found a funny bug in one of my upcoming programming projects

    size_t len = (bytecode[ip++] << 24) & 0xFF;
           len |= (bytecode[ip++] << 16) & 0xFF;
           len |= (bytecode[ip++] << 8) & 0xFF;
           len |= bytecode[ip++] & 0xFF;

when it was supposed to be

    size_t len = ((size_t)bytecode[ip++] << 24);
           len |= ((size_t)bytecode[ip++] << 16);
           len |= ((size_t)bytecode[ip++] << 8);
           len |= (size_t)bytecode[ip++];

this caused all numbers >255 being mod by 255 and I thought my foreach (called each in my languages) implementation was broken but then I was realizing it was jumping to a number too low for some reason ~20 or something when it should of been ~276

I guess, just be careful when bitshifting and don't be an idiot like me

we all make mistakes.