21
\$\begingroup\$

Your task: Print or return a conversion table with every byte from 00 to ff's value as an unsigned integer, to its value as a signed one (using two's complement).

For example:

0     0
1     1
2     2
3     3
4     4
5     5
6     6
7     7
8     8
9     9
10    10
11    11
12    12
13    13
14    14
15    15
16    16
17    17
18    18
19    19
20    20
21    21
22    22
23    23
24    24
25    25
26    26
27    27
28    28
29    29
30    30
31    31
32    32
33    33
34    34
35    35
36    36
37    37
38    38
39    39
40    40
41    41
42    42
43    43
44    44
45    45
46    46
47    47
48    48
49    49
50    50
51    51
52    52
53    53
54    54
55    55
56    56
57    57
58    58
59    59
60    60
61    61
62    62
63    63
64    64
65    65
66    66
67    67
68    68
69    69
70    70
71    71
72    72
73    73
74    74
75    75
76    76
77    77
78    78
79    79
80    80
81    81
82    82
83    83
84    84
85    85
86    86
87    87
88    88
89    89
90    90
91    91
92    92
93    93
94    94
95    95
96    96
97    97
98    98
99    99
100   100
101   101
102   102
103   103
104   104
105   105
106   106
107   107
108   108
109   109
110   110
111   111
112   112
113   113
114   114
115   115
116   116
117   117
118   118
119   119
120   120
121   121
122   122
123   123
124   124
125   125
126   126
127   127
128   -128
129   -127
130   -126
131   -125
132   -124
133   -123
134   -122
135   -121
136   -120
137   -119
138   -118
139   -117
140   -116
141   -115
142   -114
143   -113
144   -112
145   -111
146   -110
147   -109
148   -108
149   -107
150   -106
151   -105
152   -104
153   -103
154   -102
155   -101
156   -100
157   -99
158   -98
159   -97
160   -96
161   -95
162   -94
163   -93
164   -92
165   -91
166   -90
167   -89
168   -88
169   -87
170   -86
171   -85
172   -84
173   -83
174   -82
175   -81
176   -80
177   -79
178   -78
179   -77
180   -76
181   -75
182   -74
183   -73
184   -72
185   -71
186   -70
187   -69
188   -68
189   -67
190   -66
191   -65
192   -64
193   -63
194   -62
195   -61
196   -60
197   -59
198   -58
199   -57
200   -56
201   -55
202   -54
203   -53
204   -52
205   -51
206   -50
207   -49
208   -48
209   -47
210   -46
211   -45
212   -44
213   -43
214   -42
215   -41
216   -40
217   -39
218   -38
219   -37
220   -36
221   -35
222   -34
223   -33
224   -32
225   -31
226   -30
227   -29
228   -28
229   -27
230   -26
231   -25
232   -24
233   -23
234   -22
235   -21
236   -20
237   -19
238   -18
239   -17
240   -16
241   -15
242   -14
243   -13
244   -12
245   -11
246   -10
247   -9
248   -8
249   -7
250   -6
251   -5
252   -4
253   -3
254   -2
255   -1

Formatting:

  • The output doesn't have to be padded to nicely line up like it does above (just a space in between would be fine)
  • The separators can be any two strings which can't be confused for the data or each other
  • Any base can be used (but if using binary, -1 would be -00000001, not 11111111)
  • Trailing (and/or leading) whitespace and separators are allowed
  • The output must be in the correct order
  • The output must be textual; returning arrays/lists/dictionaries is not allowed

Valid outputs:

(using ... to skip over some repetitive chunks)

Zero-padded hexadecimal:

00 00
01 01
...
7f 7f
80 -80
81 -7f
...
fe -02
ff -01

Binary with , and ; for separators:

0,0;1,1;10,10;11,11;...;01111111,01111111;10000000,-10000000;10000001,-01111111;...;11111110,-00000010,11111111,00000001

Negadecimal with custom separators:

0 The HORSE is a noble animal. 0

1 The HORSE is a noble animal. 1

...

287 The HORSE is a noble animal. 287

288 The HORSE is a noble animal. 1932

289 The HORSE is a noble animal. 1933

...

354 The HORSE is a noble animal. 18

355 The HORSE is a noble animal. 19

Good luck!

\$\endgroup\$
5
  • \$\begingroup\$ Does the output have to be in the order 0..255? \$\endgroup\$
    – Neil
    Jan 15, 2021 at 14:30
  • \$\begingroup\$ @Neil It needs to be in order, yes \$\endgroup\$ Jan 15, 2021 at 14:33
  • 1
    \$\begingroup\$ Can there be leading separators? \$\endgroup\$
    – att
    Jan 15, 2021 at 20:39
  • \$\begingroup\$ @att Yes, that's fine \$\endgroup\$ Jan 16, 2021 at 0:30
  • 1
    \$\begingroup\$ @JonathanAllan Fixed, thanks \$\endgroup\$ Jan 16, 2021 at 18:40

39 Answers 39

9
\$\begingroup\$

Bash + coreutils, 30

(seq 0 127;seq -128 -1)|nl -v0

Try it online!

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Nice use of nl! \$\endgroup\$
    – user99151
    Jan 16, 2021 at 1:11
8
\$\begingroup\$

C (gcc), 54 50 47 bytes

i;main(){for(;i<256;printf("%u,%hhd ",i++,i));}

Try it online!

Explanation

i;                      // Static variable initialized to 0
main() {
    for(;i<256;         // While the variable is <= 255:
        printf("%u,%hhd ",i++,i));
                        //     Output the original, then the char-coerced
                        //     value. Increment after use.
}

C (gcc), 45 bytes

Non-portable, but 2 bytes shorter.

f(i){for(i=-1;i++<255;printf("%u,%hhd ",i));}

Try it online!

\$\endgroup\$
12
  • 3
    \$\begingroup\$ 50 bytes using %hhd \$\endgroup\$
    – EasyasPi
    Jan 15, 2021 at 14:17
  • 2
    \$\begingroup\$ You can use a static zero-initialized variable instead of argc (saves 2 bytes) \$\endgroup\$
    – Artyer
    Jan 15, 2021 at 15:03
  • 2
    \$\begingroup\$ 47 bytes (includes @Artyer 's advice). \$\endgroup\$
    – Arnauld
    Jan 15, 2021 at 15:46
  • 1
    \$\begingroup\$ 45 byte function \$\endgroup\$
    – att
    Jan 16, 2021 at 5:06
  • 2
    \$\begingroup\$ @peter-cordes It is code golf, the end justifies the means, even if it is writing blatantly non-portable code that falls apart even with -O2 \$\endgroup\$
    – EasyasPi
    Jan 17, 2021 at 0:20
5
\$\begingroup\$

APL (Dyalog Extended), 18 bytes

Full program printing to stdout with ¯ as negative symbol. Requires 0-based indexing.

(⍳∘≢,⍪)…127,-128…1

Try it online!

128…1 integers 128 through 1

- negate those

127, prepend 127

 fill integers from 0 until the first element of that (127)

() apply the tacit function:

,⍪ columnify and prepend [a column consisting of]…

⍳⍤≢ the ɩndices of the tally

\$\endgroup\$
5
\$\begingroup\$

Jelly, 11 10 bytes

⁹Ḷ_H%⁹żƊṢG

Try it online!

-1 byte thanks to Jonathan Allan

Explanation

⁹Ḷ_H%⁹żƊṢG   Main niladic link
⁹             256
 Ḷ            [0..255]
  _           Subtract
   H          half (of 256) = 128 => [-128..127]
       Ɗ      (
    %           Modulo
     ⁹          256
      ż         Zip with the list at the beginning of this inner link
       Ɗ      )
         Ṣ    Sort
          G   Format as a grid
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 10 bytes by using zip, ż to avoid : ⁹Ḷ_H%⁹żƊṢG - TIO. \$\endgroup\$ Jan 16, 2021 at 18:53
  • \$\begingroup\$ @JonathanAllan Thanks! I totally forgot Jelly had a zip dyad. \$\endgroup\$
    – xigoi
    Jan 16, 2021 at 19:10
4
\$\begingroup\$

Python 2, 37 bytes

for i in range(256):print i,127-i^127

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Julia 1.0, 31 28 bytes

r=0:255
@.println(r=>r%Int8)

Try it online!

Output format:

0 => 0
1 => 1...
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 11 10 bytes

₅ÝƵQݱ«ø»

-1 byte thanks to @ovs.

Uses single spaces and newlines as delimiters for the output.

Try it online.

Explanation:

₅Ý        # Push a list in the range [0,255]
ƵQÝ       # Push a list in the range [0,127]
   Â      # Bifurcate it; short for Duplicate & Reverse copy
    ±     # Get the bitwise-NOT of each (n → -n-1)
     «    # Merge the top two lists together
ø         # Create all possible pairs of the [0..255] and [0..127,-128..-1] lists
 »        # Join each pair by spaces, and then each string by newlines
          # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ƵQ is 127.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ -x-1 is the same as ~x: 10 bytes \$\endgroup\$
    – ovs
    Jan 15, 2021 at 14:38
  • \$\begingroup\$ @ovs Ah, of course! So obvious now that I see it, but I totally missed it. Thanks. :) \$\endgroup\$ Jan 15, 2021 at 14:42
3
\$\begingroup\$

JavaScript (ES6),  41 40 bytes

Saved 1 byte thanks to @ovs

Output format: 0,0 1,1 ... 255,-1.

f=n=>n>>8?'':[~~n,128+~n^127]+' '+f(-~n)

Try it online!

Or maybe 39 bytes or even 38 bytes by abusing the loose output format. (But it's ugly.)

Commented

f =                  // f is a recursive function
n =>                 // taking a counter n, which is initially undefined
  n >> 8 ?           // if n = 256:
    ''               //   stop the recursion
  :                  // else:
    [                //   build an array consisting of:
      ~~n,           //     the unsigned value (coerced to a number because
                     //     it's undefined on the first iteration)
      128 + ~n ^ 127 //     the signed value, which is (127 - n) XOR 127
                     //                              = (128 + (-n - 1)) XOR 127
    ]                //   end of array
    + ' '            //   coerce the array to a string and append a space
    + f(-~n)         //   append the result of a recursive call with n + 1
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 40 bytes with a slightly different formula. \$\endgroup\$
    – ovs
    Jan 15, 2021 at 15:11
  • \$\begingroup\$ @ovs Ah yes, nice one! \$\endgroup\$
    – Arnauld
    Jan 15, 2021 at 15:19
  • \$\begingroup\$ f=n=>n>>8?'':[~~n,n<<24>>24]+' '+f(-~n) \$\endgroup\$
    – tsh
    Jan 16, 2021 at 12:27
3
\$\begingroup\$

Nim -d:danger, 33 32 bytes

for b in 0..255:b.echo' ',b.int8

Try it online!

Nim, 40 39 bytes

for b in 0..255:b.echo' ',cast[int8](b)

Try it online!

Probably doesn't work on big-endian architectures.

\$\endgroup\$
3
\$\begingroup\$

Husk, 16 bytes

†szeŀ256Sṙohṡ128

Try it online!, Another method, Another method

Explanation

†szeŀ256Sṙohṡ128
            ṡ128 symmetric range of 128 [-128...128]
          oh     remove last element
        Sṙ       rotate 128 spaces
    ŀ256         range [0...255]
  ze             zip the two, creating pairs
†s               convert all the numbers to strings
                 (can also be mw)
\$\endgroup\$
3
\$\begingroup\$

Haskell, 54 bytes

unlines[show x++' ':show(x-x`div`128*256)|x<-[0..255]]

Try it online!

  • Sadly xor is not in prelude module.

  • Table is a string of pairs separated by newlines.

\$\endgroup\$
3
\$\begingroup\$

C#, 61 bytes

Not very creative, but I think the shortest this can get.

for(var i=0;i<256;)System.Console.Write(i+$",{(sbyte)i++};");

Try it online!

This is a complete program thanks to C#9 and top-level statements. SharpLab

\$\endgroup\$
2
  • \$\begingroup\$ 45 bytes using Print \$\endgroup\$
    – EasyasPi
    Jan 17, 2021 at 18:50
  • 1
    \$\begingroup\$ That's specifically C# interactive, post that as a seperate answer \$\endgroup\$ Jan 17, 2021 at 20:18
3
\$\begingroup\$

R, 37 bytes

for(i in 0:255)cat(i,i-(i>127)*256,T)

Try it online!

Separator between converted numbers is " "; separator between each element of the list of conversions is " TRUE". List is appended with a trailing separator (as allowed by the challenge rules).
"TRUE" is a pre-defined variable in R that can be specified by a single character, T, and that has non-numeric output, so it's used as a convenient golfy separator here.


R, 38 bytes

cat(paste(0:255,c(0:127,-128:-1),'
'))

Try it online!, Another approach

Previous, slightly longer attempt, but with more-conventional separators (space & newline).

\$\endgroup\$
3
\$\begingroup\$

Python 3, 47 41 bytes

-6 thanks to @EasyasPi

print([(127-i^127,i)for i in range(256)])
\$\endgroup\$
1
3
\$\begingroup\$

Java, 58 chars

void f(){for(int i=0;i<256;out.println(i+" "+(byte)i++));}

Try it online!

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

\$\endgroup\$
0
3
\$\begingroup\$

Vyxal, j, 14 9 8 bytes

₈ʁ₇ʁḂꜝJZ

Try it Online!

OP says it's allowable

-5 bytes due to porting 05AB1E

Explained

₈ʁ₇ʁḂꜝJZ
₈ʁ         # Push range [0, 256)
  ₇ʁḂ      # Push [0, 128) and (128, 0]
     ꜝ     # Bit-not over that last copy
      JZ   # Merge and zip
           # The j flag joins on newlines
\$\endgroup\$
3
\$\begingroup\$

Vyxal j, 15 bytes

₈‹ʀ₇‹ʀḂN₇NpJZvṄ

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ There's already an 8 byte answer in Vyxal j \$\endgroup\$ Jun 7, 2021 at 17:31
2
\$\begingroup\$

Python 2, 49 53 51 bytes

Saved 2 bytes taking a peek at ovs's Python answer!!!
Added 8 bytes to fix a rule violation kindly pointed out by ovs.
Saved 2 bytes thanks to ovs!!!

f=lambda i=255:i*' 'and'%d '*2%(i,127-i^127)+f(i-1)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 51 bytes by shortening the base case and using the repetition in the formatting string. \$\endgroup\$
    – ovs
    Jan 15, 2021 at 15:19
  • \$\begingroup\$ @ovs Nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Jan 15, 2021 at 15:30
2
\$\begingroup\$

PowerShell, 34 bytes

0..255|%{"$($_;($_-bxor128)-128)"}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pyth, 17 16 bytes

V256++Nd-J127xNJ

Try it online!

-1 byte thanks to FryAmTheEggman!


Python 3.8 translation:

for N in range(256):print(str(N)+" "+str((J:=127)-N^J))
\$\endgroup\$
2
  • \$\begingroup\$ Using J to reuse 127 should save a byte. Also, if not for the output restriction could save a byte by using B. \$\endgroup\$ Jan 15, 2021 at 19:03
  • \$\begingroup\$ @FryAmTheEggman thanks, I didn't realize JK assignment also returned the assigned value! \$\endgroup\$
    – hakr14
    Jan 15, 2021 at 19:27
2
\$\begingroup\$

PowerShell, 29 33 bytes

+4 bytes: fit to the task rules. Thanks @Zaelin Goodman

0..127+-128..-1|%{"$(($i++);$_)"}

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You have to also print the number being converted from; and array outputs are explicitly forbidden: The output must be textual; returning arrays/lists/dictionaries is not allowed \$\endgroup\$ Jan 15, 2021 at 18:53
2
\$\begingroup\$

Ruby, 36 bytes

256.times{|i|puts [i,127-i^127]*' '}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

AWK, 48 45 bytes

Lowered to 45 bytes thanks to Dominic van Essen.

BEGIN{for(;i<256;i++)print +i,i<128?+i:i-256}

Try it online!

Had to workaround the fact that bitwise functions in GNU AWK don't accept negative arguments. Anyway, this should work on any AWK implementation.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 45 bytes... \$\endgroup\$ Jan 16, 2021 at 13:18
  • \$\begingroup\$ @DominicvanEssen - Clearly I was overthinking that last bit. \$\endgroup\$ Jan 16, 2021 at 18:11
  • 1
    \$\begingroup\$ It seems One True Awk requires parenthesis around the conditional's test expression. Note also that no AWK I've tried requires the unary + operators on any of the print arguments. \$\endgroup\$ Jan 18, 2021 at 22:26
  • \$\begingroup\$ Also, all versions of AWK I've tried will initialize variables to 1, not zero as required. \$\endgroup\$ Jan 18, 2021 at 22:53
  • 1
    \$\begingroup\$ Ah, yes, I agree: The + will save one character of source v.s. initializing to zero. Note though that this trick of coercing the initial blank string to a zero will not work in older versions, including for example nawk from the Heirloom tools (which is arguably a bug in the older versions). This seems to have been fixed only since it was migrated to GitHub, probably in the changes dated 2018-08-22, so making it truly portable would still (and for a long time to come) require the explicit initialization to an integer. \$\endgroup\$ Jan 19, 2021 at 0:12
2
\$\begingroup\$

Scala, 46 45 bytes

()=>for(i<-0 to 255)print(s"$i,${i.toByte} ")

Output format: 0,0 1,1 2,2 ... 127,127 128,-128 129,-127 ... 253,-3 254,-2 255,-1 with , and for separators.

Try it online!

Edit: Due to conventions, the String Interpolator f was changed to s. Thanks to user!


37 bytes

... if ignoring the seperator rule.

()=>for(i<-0 to 255)print(i,i.toByte)

Output format: (0,0)(1,1)(2,2)(...)(127,127)(128,-128)(129,-127)(...)(253,-3)(254,-2)(255,-1) with , and )( for separators, but unfortunately also with a leading ( and trailing ).

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice! Btw, s is usually used instead of f. \$\endgroup\$
    – user
    Jan 16, 2021 at 14:58
2
\$\begingroup\$

FALSE, 37 bytes

0[$$$$256=~][128>[256-]?\.44,.10,1+]#

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 38 bytes

for i in range(256):print(i,127-i^127)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PHP, 64 bytes

<?php for($x=0;$x<256;$x++){echo$x.','.($x<128?$x:$x-256).";";}
<?php for($x = 0; $x < 256; $x++){ #Go through each number
  echo $x . ',' . #Write x, then...
($x < 128 ? $x #if x is less than 128, write x again, 
: $x - 256) . ";";  #otherwise write x - 256 
}

Uses ',' and '.' as separators.

\$\endgroup\$
3
  • \$\begingroup\$ You not have to count the <?php tag as long as your code is runnable by php -r; as the loop repeats a single instruction, no need for the braces; if you output separate values instead of concatenating them, the parenthesis are not necessary either; you can move the incrementing of the counter to the echo, just need to change the range. BTW, if you use a literal newline character the output is more readable at the same length. Try it online! \$\endgroup\$
    – manatwork
    Feb 1, 2021 at 4:51
  • \$\begingroup\$ Using expression instead of the ternary operator allows squeezing it a bit more. Try it online! \$\endgroup\$
    – manatwork
    Feb 1, 2021 at 5:04
  • \$\begingroup\$ @manatwork Nice, thanks! \$\endgroup\$
    – emanresu A
    Feb 1, 2021 at 8:50
2
\$\begingroup\$

Factor, 43 bytes

256 iota [ dup pprint bl 8 >signed . ] each

Try it online!

\$\endgroup\$
2
\$\begingroup\$

jq, 30 bytes

range(256)|[.,(128+.)%256-128]

Try it online!

output format:

[
  0,
  0
]
...
[
  255,
  -1
]
\$\endgroup\$
1
\$\begingroup\$

Red, 45 bytes

repeat n 256[print[p: n - 1 p xor 128 - 128]]

Try it online!

Uses @Arnauld's formula

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.