| Bytes | Lang | Time | Link |
|---|---|---|---|
| 084 | Python 3 | 250408T235906Z | CrSb0001 |
| 036 | CASIO BASIC BASE CASIO fx9750GIII | 250425T150042Z | madeforl |
| 049 | Perl 5 | 240925T001521Z | Kjetil S |
| 128 | Python 3 | 240903T161903Z | Digital |
| 050 | Python 2 | 240830T114323Z | Lucenapo |
| 097 | C gcc | 240830T170906Z | Digital |
| 078 | F# | 240831T120047Z | Juuz |
| 053 | C gcc | 240829T203006Z | jdt |
| 012 | Vyxal 3 | 240830T122450Z | pacman25 |
| 013 | 05AB1E | 240830T073517Z | Kevin Cr |
| 037 | JavaScript ES6 | 240829T171331Z | Arnauld |
| 023 | Charcoal | 240829T232654Z | Neil |
Python 3, 129 121 106 104 98 94 84 bytes
-7 bytes
-15 bytes
-2 bytes
-6 bytes
-4 bytes thanks to @ceilingcat
-10 bytes because I reread the rules again
Better than @DigitalTrauma's 128 byte solution, and doesn't use any modules.
def w(v):
v+=(2+(v<-2**31)<<31)*(v<0)
while~127&v:print(v&127|128);v>>=7
print(v)
Try it online! (129 byte solution)
Try it online! (94 byte solution)
Try it online! (84 byte solution)
Prints each byte in order on each newline.
IIRC my original solution was 144 bytes, but there was an issue with values \$-2^{32}\le\text{val}\lt-2^{31}\$ not giving the correct value, so I ended up wasting 15 bytes there. However I did end up being able to golf another 30 bytes from there, which brought us to the 129 byte solution.
Then I realized I could simply print each byte on each newline, saving me 8 bytes.
Now, since I break when ~127&v<1, I realized I could just write the while loop as while~127&v (formerly while~127%v>0, which had lost me 2 bytes) and then print(hex(v)) once that was untrue, saving 15 bytes.
CASIO BASIC BASE (CASIO fx9750GIII), 36 bytes
BASEmode is an option you can choose when creating a program that allows the program to manipulate number bases but severely limits the instruction set. not even exponents!
this does not support negative numbers due to calculator limitations
?→A
Lbl A
Aand127
A÷128→A
A⟹Ansor128
Ans▶Hex◢
A⟹Goto A
Python 3, 128 bytes
Attempt to get an existing library to do the heavy lifting:
from google.protobuf.internal.encoder import _VarintEncoder as v
def e(n):
b=bytearray()
v()(b.extend,n&(2**32-1),0)
return b
Don't Try it online! - TIO would need google.protobuf modules installed.
C (gcc), 97 bytes
- Saved 2 bytes, thanks to @jdt
- Saved 16 bytes, thanks to @ceilingcat
Most of the other entries seem to be taking a recursive approach. I was curious to try a direct approach using x86 instruction intrinsics.
Returns the output in a uint64_t which can be cast to a 0-terminated char array.
#define f(n)_pdep_u64(n+0U,~(~0LU/510))|_pdep_u64((1<<((63-__builtin_clzll(n+0U))/7))-1,~0LU/510)
F#, 78 bytes
let rec e v=
let b=v&&&127u
if b=v then[byte b]else byte(b|||128u)::e(v>>>7)
A function taking a unsigned 32-bit int (treated as a signed int with the same bits) and returning a list of bytes.
C (gcc), 62 59 53 bytes
f(n){printf("%02X ",n&127|!!(n=n+0U>>7)<<7);n&&f(n);}
The literal 0U is an unsigned integer constant. Adding it to n promotes n to an unsigned type.
-6 bytes thanks to @Arnauld!
Vyxal 3, 12 bytes
₃E%₆yDκ≠₆×+Ṛ
Port of Charcoal, but there's a one byte built in for 128 so it beats 05ab1e lol
05AB1E, 13 bytes
žJ%žyвā≠žy*+R
Port of @Neil's Charcoal answer, so make sure to upvote that answer as well!
Try it online or verify all test cases.
Explanation:
% # Modulo the (implicit) input-integer by
žJ # constant 4294967296
в # Then convert it from a base-10 integer to a list in base
žy # constant 128
ā # Push a list in the range [1,length] (without popping)
≠ # Check for each that it's NOT 1, so we now have a list [0,1,1,1,...]
* # Multiply each by
žy # constant 128
+ # Add that to the values at the same positions in the list
R # Then reverse the list
# (after which it is output implicitly as result)
JavaScript (ES6), 37 bytes
Expects an integer and returns an array of bytes.
f=n=>n>>7?[n&127|128,...f(n>>>7)]:[n]
Commented
f = n => // f is a recursive function taking an integer n
n >> 7 ? // if any bit at position 8 .. 32 of n is set:
[ // append:
n & 127 // the 7 least significant bits of n
| 128, // with the 8-th bit forced to 1
...f( // followed by the result of a recursive call:
n >>> 7 // pass n right-shifted by 7 positions,
// with zero bits shifted in from the left
) // end of recursive call
] // end of array
: // else:
[ n ] // append n and stop the recursion
Charcoal, 23 bytes
∨I⮌E↨﹪NX²I32¹²⁸⁺ι∧κ¹²⁸0
Try it online! Link is to verbose version of code. Explanation:
N Input integer
﹪ Modulo
² Literal integer `2`
X Raised to power
32 Literal string `32`
I Cast to integer
↨ Convert to base
¹²⁸ Literal integer `128`
E Map over list
ι Current value
⁺ Plus
κ Current index
∧ Logical And
¹²⁸ Literal integer `128`
⮌ Reversed
I Cast to string
∨ Logical Or
0 Literal string `0`
Implicitly print