| Bytes | Lang | Time | Link |
|---|---|---|---|
| 009 | Japt ! | 240812T222143Z | Shaggy |
| 034 | C gcc | 240803T130133Z | Maya |
| 084 | Desmos | 240719T014706Z | W D |
| 558 | Uiua | 240723T054957Z | Europe20 |
| 054 | C gcc | 240722T012045Z | Digital |
| 009 | MATL | 240717T162439Z | Luis Men |
| 064 | Java | 240719T190530Z | swpalmer |
| 036 | Raku Perl 6 rakudo | 240719T071100Z | bb94 |
| 068 | Go | 240718T005925Z | bigyihsu |
| 030 | JavaScript ES6 | 240717T160651Z | Arnauld |
| 036 | Python 3 | 240717T141641Z | Jitse |
| 016 | K ngn/k | 240719T041846Z | akamayu |
| 018 | x8664 machine code function | 240718T195509Z | Peter Co |
| 007 | Jelly | 240717T141820Z | hyper-ne |
| 024 | K ngn/k | 240718T135912Z | Ashy |
| 006 | Vyxal | 240717T140602Z | lyxal |
| 147 | Nibbles | 240718T092631Z | Dominic |
| 044 | Bash | 240717T151013Z | Themooni |
| 040 | R | 240717T152918Z | Dominic |
| 009 | 05AB1E | 240718T064459Z | Kevin Cr |
| 030 | JavaScript Node.js | 240718T032003Z | l4m2 |
| 011 | Charcoal | 240717T213655Z | Neil |
| 036 | APL+WIN | 240717T182357Z | Graham |
Japt -!, 9 bytes
;s#òd)kE
;s#òd)kE :Implicit input of integer
s :Convert to string in base
# : 127 (note the unprintable after the #)
ò : Range [0,127]
d : Character at each codepoint
) :End base conversion
k :Remove characters in
; E : ASCII
:Implicit output of logical NOT of resulting string
Desmos, 84105 bytes
Thanks to Aiden Chow for removing B(x) and 21 bytes
k=128
f(n)=\left\{31<mod(floor(n/k^{[0...floor(log_k(n+0^n))]}),k)<k-1,0\right\}.min
Try it on Desmos (with test cases)
Explanation
f(x) checks each character's printability, returning 1 for printable text and 0 for unprintable text.
If you are pasting this in to try yourself, you may need to "activate" the f(n) equation by focusing (clicking) on it and pressing either the left or right arrow key on your keyboard or the built-in Desmos keyboard. This is likely because I don't use the backslashes in \join, \floor, \mod etc. (not sure how Desmos breaks when this happens though), so this solution is susceptible to Desmos bugfixes breaking my code I guess
Or you can paste in the f(n) line individually because that works I guess
Uiua, 55 bytes (UTF-8), 30 chars
c ← 128
/↧∊:+32⇡95⌊◿c÷ⁿ:c⇡⌈ₙc.
C (gcc), 54
Non recursive solution:
f(n,a){a=1;for(n=n?:1;n&&(a=isprint(n&127));n>>=7);a;}
C (gcc), 55
Recursive solution is 1 byte longer:
g(n){n=n?isprint(n&127)?g(n>>=7):0:1;}f(n){n=n?g(n):0;}
MATL, 10 9 bytes
7W_YA6Y2m
Outputs a vector of ones (which is truthy) or a vector containing at least a zero (which is falsy) depending on whether the result only uses printable ASCII or not.
Try it online! Or verify all test cases (the code includes test for truthiness/falsihood).
Explanation
% Implicit input
7W % 7, base-2 exponentiation: gives 128
_YA % Convert to base 128
6Y2 % Push ASCII range (predefined literal): ' ':'~'
m % Ismember (element-wise)
% Implicit display
Java, 64 bytes
x->{if(x==0)x=1;while(x%128!=127&&x%128>31){x>>=7;}return x==0;}
Raku (Perl 6) (rakudo), 41 36 bytes
Returns 0 for false and a non-zero integer for true.
{$_*?(127>.polymod(128 xx*).all>31)}
Go, 75 68 bytes
func f(n int)bool{for k:=n;k>0;k>>=7{if -^k%128<33{n=0}}
return n>0}
Based on @Jitse's answer.
Does not swap true and false.
- -7 bytes by @ovs, return the first
returnwithn=0, forcing the finalreturntofalse.
JavaScript (ES6), 30 bytes
Very similar to Jitse's answer.
Returns 0 for printable or true for non-printable.
f=n=>-~n%128<33||(n>>=7)&&f(n)
JavaScript (ES6), 27 bytes
-2 thanks to @tsh
Returns 0 for printable / truthy for non-printable.
f=n=>-~n%128<33||f(n>>=7)*n
Python 3, 36 bytes
f=lambda n:-~n%128<33or f(n>>7)*n>>7
Outputs with truthy and falsey swapped
-2 bytes thanks to Themoonisacheese
-2 bytes thanks to squareroot12621
-2 bytes thanks to Albert.Lang
-1 byte thanks to Jonathan Allan
K (ngn/k), 16 bytes
1~&/~32 127'128\
128\ /decode in base 128
32 127' /-1 if <32, 0 if 32<=x<127, 1 if 127<x
&/~ /all 0?
1~ /is 1? (since the unit element for & is 9223372036854775807
x86-64 machine code function, 18 bytes
Integer arg in ECX, bool return value in AL (actually zero-extended to RAX). If I'd used EDI for the arg, this would be compatible with the x86-64 System V calling convention. As-is, it's Windows x64.
NASM listing trimmed to show hexdump of machine code and source.
; takes a string of ASCII digits packed into a 32-bit integer
packedascii_is_printable:
.loop:
8D4101 lea eax, [rcx+1] ; wrap 0x7f to 0x80 = -128 if we use a signed compare. Or to 0 since this is before AND
; 32-bit alternative mov eax, ecx (2 bytes) ; inc eax (1 byte) before or after AND
247F and al, 0x7F ; isolate low 7 bits - one ASCII character
3C21 cmp al, 0x21 ; increment by one mapped the printable range to 0x21 - 0x7f from the usual 0x20 - 0x7e
0F9DC0 setge al ; set return value according to most recent character checked
7C05 jl .found_false ; early out on first false, else keep checking
C1E907 shr ecx, 7
75EF jnz .loop ; leave the loop after processing all the set bits, since our input numbers aren't actually fixed-width in bits.
.found_false:
C3 ret
; return value in AL (zero extended to EAX, actually, since we don't exit the loop until high bits of ECX are clear)
We add 1 and copy the low 7-bit character into AL, and isolate it with AND. Then check if it's 0x21 to 0x7f, i.e. if the original was 0x20 to 0x7e. Since every 7-bit value is <= 0x7f, we only have to check the low end of the range, hence adding 1.
This isn't as compact as I'd like, it feels like there should be a way to avoid both a branch and a 3-byte setcc. Perhaps I could make use 32-bit mode for salc if I can use an unsigned compare. (Undocumented opcode that sets AL according to CF). I think I can since we cmp after clearing the high bit.
Jelly, 7 bytes
bØ⁷ỌḟØṖ
bØ⁷ỌḟØṖ main link
b convert to base
Ø⁷ 128
Ọ convert to characters
ḟ filter to remove
ØṖ printable ASCII
If anything is left after the filter (i.e. the return value is a non-empty list), then it's the false case. If the filter removes everything (i.e. the return value is an empty list), then it's the true case. The footer uses Ṇ (logical NOT) to map [] -> true and [...] -> false.
Nibbles, 14 nibbles (7 bytes)
Program bytes:
d9 87 12 08 3a dd 3b
Human-readable representation:
,|`@128$\ch$P
Outputs zero (falsy) for only-printable-ASCII input, or a positive integer (truthy) for input containing unprintable base-128 digits.
Explanation:
,|`@128$\ch$P
, # length of
| # keep
\ P # only unprintable ASCII
ch$ # from character representations of
$ # input
`@128 # as base-128 digits
Note:
Typically, Nibbles programs are written using human-readable (kind-of) tokens representing single-nibble or two-nibble elements. The Nibbles interpreter should then encode these into the final, runnable program code.
In this case, though, for unknown (to me) reasons, the Nibbles interpreter outputs a warning and refuses to encode this program, even though it runs it correctly. Nevertheless, it is possible to manually write the 7 program bytes d9 87 12 08 3a dd 3b directly to a file, and run this, as shown.
This is not the normal way to write or run a Nibbles program, but it works.
Bash, 54 44 bytes
-10 bytes by Dominic van Essen and GammaFunction
(($1%128%127>31))&&((($1<128))||$0 $[$1>>7])
Use as a bash script. Takes in number as first argument, returns with 1 for false and 0 for true (bash convention).
ATO link assumes the function is named f because $0 doesn't work in ATO, but the penalty for hardcoding the script name would be a single byte, so the score would be the same.
Turns out this is a port of @Jitse's answer, but i came up with it independently.
05AB1E, 9 bytes
žQÇIžyвåP
Try it online or verify all test cases.
Explanation:
žQ # Push printable ASCII constant: " !"...|}~"
Ç # Convert it to a list of codepoint integers: [32,126]
I # Push the input-integer
žyв # Convert the (base-10) input to a base-128 list
å # Check for each value if it's in the [32,126]-list
P # Check if all are truthy
# (which is output implicitly as result)
JavaScript (Node.js), 30 bytes
f=n=>-~n%128<33?n<!!n:f(n/128)
Same length with Arnauld but somehow nicer output
JavaScript (Node.js), 26 bytes
f=n=>-~n%128<33?!n:f(n>>7)
Worse input
Charcoal, 11 bytes
⌊⍘N⭆¹²⁸№γ℅ι
Try it online! Link is to verbose version of code. Actually outputs 1 or 0 for once. Explanation:
N Input as a number
⍘ Convert to custom string base
¹²⁸ Literal integer `128`
⭆ Map over implicit range and join
№ Count of
ι Current value
℅ Convert to Unicode
γ In printable ASCII
⌊ Take the minimum
Implicitly print
Would have been 10 bytes if the output for 0 was truthy as @xnor suggests:
⬤↨N¹²⁸№γ℅ι
Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. 1 if all of the characters a printable ASCII, nothing if not. Explanation:
N Input as a number
↨ Converted to base
¹²⁸ Literal integer `128`
⬤ All values satisfy
№ Count of
ι Current value
℅ Convert to Unicode
γ In printable ASCII
Implicitly print
APL+WIN, 36 bytes
Prompts for integer. Index origin=0. 1=true, 0=false
(+/v∊32+⍳95)=⍴v←((⌈128⍟2⌈n)⍴128)⊤n←⎕
