| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Integers - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Integers - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html" aria-current="page">Integers</a></li> |
| 25 | <li><a href="values.html">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Raz · lesson 3 of 15</p><h1>Integers</h1> |
| 42 | <p>raz has ten integer types, and a program that works with bytes, addresses, hashes and file offsets uses most of them. Each is a different type, so each has rules, and the rules are the same everywhere: that is the point of having them.</p> |
| 43 | <table><thead><tr><th>Type</th><th>Size</th><th>Values</th></tr></thead><tbody><tr><td data-label="Type"><code>I8</code> <code>I16</code> <code>I32</code> <code>I64</code></td><td data-label="Size">1, 2, 4, 8 bytes</td><td data-label="Values">signed, two's complement</td></tr><tr><td data-label="Type"><code>U8</code> <code>U16</code> <code>U32</code> <code>U64</code></td><td data-label="Size">1, 2, 4, 8 bytes</td><td data-label="Values">unsigned</td></tr><tr><td data-label="Type"><code>INT</code></td><td data-label="Size">an address's: 8 on x86-64</td><td data-label="Values">signed, as big as an address</td></tr><tr><td data-label="Type"><code>UINT</code></td><td data-label="Size">an address's</td><td data-label="Values">unsigned, as big as an address</td></tr></tbody></table> |
| 44 | <p><code>SIZE(T)</code> says how many bytes a type takes, and an unsigned type's largest value is what you get by taking one from zero. This program asks each type.</p> |
| 45 | <div class="file"><span>Family.raz</span></div><pre class="code"><code><span class="kw">module</span> Family; |
| 46 | <span class="kw">import</span> Out; |
| 47 | |
| 48 | <span class="kw">proc</span> main*; |
| 49 | <span class="kw">var</span> a: <span class="ty">U8</span>; b: <span class="ty">U16</span>; c: <span class="ty">U32</span>; d: <span class="ty">U64</span>; |
| 50 | <span class="kw">begin</span> |
| 51 | a := <span class="number">0</span>; a := a - <span class="number">1</span>; |
| 52 | b := <span class="number">0</span>; b := b - <span class="number">1</span>; |
| 53 | c := <span class="number">0</span>; c := c - <span class="number">1</span>; |
| 54 | d := <span class="number">0</span>; d := d - <span class="number">1</span>; |
| 55 | Out.str(<span class="string">"U8 "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U8</span>)); Out.str(<span class="string">" byte, largest "</span>); Out.int(<span class="ty">I64</span>(a)); Out.ln; |
| 56 | Out.str(<span class="string">"U16 "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U16</span>)); Out.str(<span class="string">" bytes, largest "</span>); Out.int(<span class="ty">I64</span>(b)); Out.ln; |
| 57 | Out.str(<span class="string">"U32 "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U32</span>)); Out.str(<span class="string">" bytes, largest "</span>); Out.int(<span class="ty">I64</span>(c)); Out.ln; |
| 58 | Out.str(<span class="string">"U64 "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U64</span>)); Out.str(<span class="string">" bytes, largest 0x"</span>); Out.hex(d, <span class="number">16</span>); Out.ln; |
| 59 | Out.str(<span class="string">"INT "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">INT</span>)); Out.str(<span class="string">" bytes, as big as an address\n"</span>); |
| 60 | Out.str(<span class="string">"I8 "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">I8</span>)); Out.str(<span class="string">" byte, from -128 to 127\n"</span>); |
| 61 | <span class="kw">end</span>; |
| 62 | |
| 63 | <span class="kw">end</span> Family.</code></pre> |
| 64 | <pre class="output"><code>U8 1 byte, largest 255 |
| 65 | U16 2 bytes, largest 65535 |
| 66 | U32 4 bytes, largest 4294967295 |
| 67 | U64 8 bytes, largest 0xFFFFFFFFFFFFFFFF |
| 68 | INT 8 bytes, as big as an address |
| 69 | I8 1 byte, from -128 to 127 |
| 70 | </code></pre> |
| 71 | <h2>INT is the everyday one</h2> |
| 72 | <p>Lengths, indexes, counts and sizes are <code>INT</code>s. <code>LEN(s)</code> of a slice is an <code>INT</code>, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for <code>INT</code> unless you have a reason: a byte is a <code>U8</code>, a character an <code>I32</code>, a file's size an <code>I64</code>, a hash a <code>U64</code>.</p> |
| 73 | <p><code>INT</code> and <code>I64</code> are two types even where they have the same size. An <code>INT</code> goes silently into an <code>I64</code> (and a <code>UINT</code> into a <code>U64</code>) wherever it meets one, in <code>pos + LEN(s)</code> as in a call, which is why <code>Out.int(n)</code> and <code>Fmt.int(n)</code> take an <code>I64</code> and accept any <code>INT</code>. The other way is written, <code>INT(pos)</code>, since an <code>INT</code> may be smaller on another machine. An <code>INT</code> holds every <code>U8</code>, <code>I8</code> and <code>I16</code> on any machine, so those go silently into an <code>INT</code> too (and a <code>U8</code> or <code>U16</code> into a <code>UINT</code>): <code>n := s[i]</code>, and <code>n * 10 + s[i] - '0'</code> for a digit. Not what an operator made of them, which was computed in the small type and may have wrapped there: with <code>a, b: U8</code>, <code>n := a + b</code> is an error, and <code>INT(a) + b</code> the sum. And only one step: a <code>U8</code> goes into an <code>INT</code>, not on into an <code>I64</code>, so <code>Out.int(I64(b))</code> still says it. Nothing else converts unasked.</p> |
| 74 | <h2>A number written in the program</h2> |
| 75 | <p>A number in the program text is untyped until it meets a type, and then it must fit. <code>0FFH</code> is hexadecimal (it starts with a digit and ends in <code>H</code>), a character in quotes is its code point, and <code>'\u{E9}'</code> is a code point written by number. A hexadecimal literal too big for an <code>I64</code> is a <code>U64</code>.</p> |
| 76 | <div class="file"><span>Literals.raz</span></div><pre class="code"><code><span class="kw">module</span> Literals; |
| 77 | <span class="kw">import</span> Out; |
| 78 | |
| 79 | <span class="kw">proc</span> main*; |
| 80 | <span class="kw">var</span> b: <span class="ty">U8</span>; w: <span class="ty">I16</span>; big: <span class="ty">U64</span>; |
| 81 | <span class="kw">begin</span> |
| 82 | b := <span class="number">0FFH</span>; <span class="comment">(* fits a U8 *)</span> |
| 83 | w := -<span class="number">300</span>; |
| 84 | big := <span class="number">0FFFFFFFFFFFFFFFFH</span>; <span class="comment">(* above the largest I64: a U64 *)</span> |
| 85 | Out.int(<span class="ty">I64</span>(b)); Out.char(<span class="string">' '</span>); |
| 86 | Out.int(<span class="ty">I64</span>(w)); Out.char(<span class="string">' '</span>); |
| 87 | Out.int(<span class="ty">I64</span>(<span class="string">'a'</span>)); Out.char(<span class="string">' '</span>); |
| 88 | Out.int(<span class="ty">I64</span>(<span class="string">'\u{E9}'</span>)); Out.char(<span class="string">' '</span>); |
| 89 | Out.hex(big, <span class="number">16</span>); Out.ln; |
| 90 | <span class="kw">end</span>; |
| 91 | |
| 92 | <span class="kw">end</span> Literals.</code></pre> |
| 93 | <pre class="output"><code>255 -300 97 233 FFFFFFFFFFFFFFFF |
| 94 | </code></pre> |
| 95 | <p>A constant that doesn't fit is an error, not a quiet cut. A conversion of a constant, which says you mean it, is allowed: <code>U8(300)</code> is 44.</p> |
| 96 | <div class="file"><span>Fits.raz</span></div><pre class="code"><code><span class="kw">module</span> Fits; |
| 97 | |
| 98 | <span class="kw">proc</span> main*; |
| 99 | <span class="kw">var</span> b: <span class="ty">U8</span>; c: <span class="ty">U8</span>; |
| 100 | <span class="kw">begin</span> |
| 101 | b := <span class="number">300</span>; |
| 102 | c := <span class="ty">U8</span>(<span class="number">300</span>); |
| 103 | <span class="kw">end</span>; |
| 104 | |
| 105 | <span class="kw">end</span> Fits.</code></pre> |
| 106 | <pre class="output term"><code><span class="cmd">$ raz Fits.raz</span> |
| 107 | Fits.raz:6:10: error: 300 doesn't fit U8 |
| 108 | 6 | b := 300; |
| 109 | | ^^^ |
| 110 | raz: 1 error</code></pre> |
| 111 | <h2>Wrapping</h2> |
| 112 | <p>Arithmetic wraps at the type's width and never stops the program. <code>250 + 10</code> in a <code>U8</code> is 4; one more than the largest <code>I8</code> is the smallest. A unary minus wraps too: for an unsigned <code>x</code>, <code>-x</code> is <code>0 - x</code>. This is not undefined behaviour, as in C; it is what the report says, in every program, on every target, so a hash function can rely on it.</p> |
| 113 | <div class="file"><span>Wrap.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrap; |
| 114 | <span class="kw">import</span> Out; |
| 115 | |
| 116 | <span class="kw">proc</span> main*; |
| 117 | <span class="kw">var</span> a: <span class="ty">U8</span>; b: <span class="ty">I8</span>; c: <span class="ty">U32</span>; d: <span class="ty">U8</span>; |
| 118 | <span class="kw">begin</span> |
| 119 | a := <span class="number">250</span>; a := a + <span class="number">10</span>; |
| 120 | Out.int(<span class="ty">I64</span>(a)); Out.ln; <span class="comment">(* 4 *)</span> |
| 121 | b := <span class="number">127</span>; b := b + <span class="number">1</span>; |
| 122 | Out.int(<span class="ty">I64</span>(b)); Out.ln; <span class="comment">(* -128 *)</span> |
| 123 | c := <span class="number">0</span>; c := c - <span class="number">1</span>; |
| 124 | Out.int(<span class="ty">I64</span>(c)); Out.ln; <span class="comment">(* 4294967295 *)</span> |
| 125 | d := <span class="number">1</span>; d := -d; |
| 126 | Out.int(<span class="ty">I64</span>(d)); Out.ln; <span class="comment">(* 255: 0 - 1 *)</span> |
| 127 | <span class="kw">end</span>; |
| 128 | |
| 129 | <span class="kw">end</span> Wrap.</code></pre> |
| 130 | <pre class="output"><code>4 |
| 131 | -128 |
| 132 | 4294967295 |
| 133 | 255 |
| 134 | </code></pre> |
| 135 | <p>Because wrapping is defined, a hash that depends on it is portable. Here is the FNV-1a hash that <code>Str.hash</code> uses, written in a few lines; the multiplication overflows a <code>U64</code> on every byte, on purpose.</p> |
| 136 | <div class="file"><span>Fnv.raz</span></div><pre class="code"><code><span class="kw">module</span> Fnv; |
| 137 | <span class="kw">import</span> Out, Str; |
| 138 | |
| 139 | <span class="kw">const</span> Offset = <span class="number">0CBF29CE484222325H</span>; |
| 140 | <span class="kw">const</span> Prime = <span class="number">100000001B3H</span>; |
| 141 | |
| 142 | <span class="comment">(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)</span> |
| 143 | <span class="kw">proc</span> fnv(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">U64</span>; |
| 144 | <span class="kw">var</span> h: <span class="ty">U64</span>; i: <span class="ty">INT</span>; |
| 145 | <span class="kw">begin</span> |
| 146 | h := Offset; |
| 147 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> |
| 148 | h := (h <span class="kw">xor</span> <span class="ty">U64</span>(s[i])) * Prime; |
| 149 | <span class="kw">end</span>; |
| 150 | <span class="kw">return</span> h; |
| 151 | <span class="kw">end</span>; |
| 152 | |
| 153 | <span class="kw">proc</span> main*; |
| 154 | <span class="kw">begin</span> |
| 155 | Out.hex(fnv(<span class="string">"raz"</span>), <span class="number">16</span>); Out.ln; |
| 156 | Out.hex(Str.hash(<span class="string">"raz"</span>), <span class="number">16</span>); Out.ln; |
| 157 | <span class="kw">if</span> fnv(<span class="string">"raz"</span>) = Str.hash(<span class="string">"raz"</span>) <span class="kw">then</span> Out.str(<span class="string">"the same\n"</span>); <span class="kw">end</span>; |
| 158 | <span class="kw">end</span>; |
| 159 | |
| 160 | <span class="kw">end</span> Fnv.</code></pre> |
| 161 | <pre class="output"><code>89F6BC1960FF4912 |
| 162 | 89F6BC1960FF4912 |
| 163 | the same |
| 164 | </code></pre> |
| 165 | <h2>Widening, narrowing, and sign</h2> |
| 166 | <p>A conversion to a narrower integer cuts the value to the new width. To a wider one it extends by the sign for a signed source and with zeros for an unsigned one. That difference is the whole story of "why did my byte come out negative".</p> |
| 167 | <div class="file"><span>Widths.raz</span></div><pre class="code"><code><span class="kw">module</span> Widths; |
| 168 | <span class="kw">import</span> Out; |
| 169 | |
| 170 | <span class="kw">proc</span> main*; |
| 171 | <span class="kw">var</span> s: <span class="ty">I8</span>; u: <span class="ty">U8</span>; |
| 172 | <span class="kw">begin</span> |
| 173 | s := -<span class="number">1</span>; |
| 174 | u := <span class="number">255</span>; <span class="comment">(* the same eight bits *)</span> |
| 175 | Out.int(<span class="ty">I64</span>(s)); Out.char(<span class="string">' '</span>); <span class="comment">(* sign-extended: -1 *)</span> |
| 176 | Out.int(<span class="ty">I64</span>(u)); Out.char(<span class="string">' '</span>); <span class="comment">(* zero-extended: 255 *)</span> |
| 177 | Out.int(<span class="ty">I64</span>(<span class="ty">U8</span>(s))); Out.char(<span class="string">' '</span>); <span class="comment">(* the bits, read as unsigned: 255 *)</span> |
| 178 | Out.int(<span class="ty">I64</span>(<span class="ty">I8</span>(u))); Out.char(<span class="string">' '</span>); <span class="comment">(* and back: -1 *)</span> |
| 179 | Out.int(<span class="ty">I64</span>(<span class="ty">U8</span>(<span class="number">300</span> + <span class="ty">INT</span>(u)))); Out.ln; <span class="comment">(* 555 cut to 8 bits: 43 *)</span> |
| 180 | <span class="kw">end</span>; |
| 181 | |
| 182 | <span class="kw">end</span> Widths.</code></pre> |
| 183 | <pre class="output"><code>-1 255 255 -1 43 |
| 184 | </code></pre> |
| 185 | <p>Relations compare as signed or unsigned according to the type, so the same bits order differently:</p> |
| 186 | <div class="file"><span>Order.raz</span></div><pre class="code"><code><span class="kw">module</span> Order; |
| 187 | <span class="kw">import</span> Out; |
| 188 | |
| 189 | <span class="kw">proc</span> main*; |
| 190 | <span class="kw">var</span> s: <span class="ty">I8</span>; u: <span class="ty">U8</span>; |
| 191 | <span class="kw">begin</span> |
| 192 | s := -<span class="number">1</span>; |
| 193 | u := <span class="ty">U8</span>(s); <span class="comment">(* 255 *)</span> |
| 194 | <span class="kw">if</span> s < <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">"s is below zero\n"</span>); <span class="kw">end</span>; |
| 195 | <span class="kw">if</span> u > <span class="number">100</span> <span class="kw">then</span> Out.str(<span class="string">"u is above 100\n"</span>); <span class="kw">end</span>; |
| 196 | <span class="kw">if</span> <span class="ty">U8</span>(s) = u <span class="kw">then</span> Out.str(<span class="string">"but they are the same bits\n"</span>); <span class="kw">end</span>; |
| 197 | <span class="kw">end</span>; |
| 198 | |
| 199 | <span class="kw">end</span> Order.</code></pre> |
| 200 | <pre class="output"><code>s is below zero |
| 201 | u is above 100 |
| 202 | but they are the same bits |
| 203 | </code></pre> |
| 204 | <h2>Division</h2> |
| 205 | <p><code>div</code> rounds toward zero, and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is -3 and <code>-7 mod 2</code> is -1. Dividing by zero stops the program, and so does dividing the smallest value of a signed type by -1, whose result can't be represented. Neither is a silent wrong answer.</p> |
| 206 | <div class="file"><span>Divide.raz</span></div><pre class="code"><code><span class="kw">module</span> Divide; |
| 207 | <span class="kw">import</span> Out; |
| 208 | |
| 209 | <span class="kw">proc</span> main*; |
| 210 | <span class="kw">var</span> n, d: <span class="ty">INT</span>; |
| 211 | <span class="kw">begin</span> |
| 212 | n := -<span class="number">7</span>; d := <span class="number">2</span>; |
| 213 | Out.int(n <span class="kw">div</span> d); Out.char(<span class="string">' '</span>); Out.int(n <span class="kw">mod</span> d); Out.char(<span class="string">' '</span>); <span class="comment">(* -3 -1 *)</span> |
| 214 | n := <span class="number">7</span>; d := -<span class="number">2</span>; |
| 215 | Out.int(n <span class="kw">div</span> d); Out.char(<span class="string">' '</span>); Out.int(n <span class="kw">mod</span> d); Out.ln; <span class="comment">(* -3 1 *)</span> |
| 216 | <span class="kw">end</span>; |
| 217 | |
| 218 | <span class="kw">end</span> Divide.</code></pre> |
| 219 | <pre class="output"><code>-3 -1 -3 1 |
| 220 | </code></pre> |
| 221 | <h2>Shifts and bits</h2> |
| 222 | <p><code>&</code> (and), <code>|</code> (or), <code>xor</code> and <code>~</code> (not) work on every bit. <code>x shl n</code> and <code>x shr n</code> shift by <code>n</code> modulo the width of <code>x</code>'s type: for a <code>U8</code>, <code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in copies of the sign bit for signed types, so it divides by two keeping the sign, and zeros for unsigned ones. The result has <code>x</code>'s type, cut to its width: in a <code>U8</code>, <code>0C0H shl 1</code> is <code>80H</code>.</p> |
| 223 | <div class="file"><span>Shifts.raz</span></div><pre class="code"><code><span class="kw">module</span> Shifts; |
| 224 | <span class="kw">import</span> Out; |
| 225 | |
| 226 | <span class="kw">proc</span> main*; |
| 227 | <span class="kw">var</span> u: <span class="ty">U8</span>; s: <span class="ty">I8</span>; |
| 228 | <span class="kw">begin</span> |
| 229 | u := <span class="number">0C0H</span>; |
| 230 | Out.hex(<span class="ty">U64</span>(u <span class="kw">shl</span> <span class="number">1</span>), <span class="number">2</span>); Out.char(<span class="string">' '</span>); <span class="comment">(* 80: cut to 8 bits *)</span> |
| 231 | Out.hex(<span class="ty">U64</span>(u <span class="kw">shr</span> <span class="number">4</span>), <span class="number">2</span>); Out.char(<span class="string">' '</span>); <span class="comment">(* 0C: zeros come in *)</span> |
| 232 | Out.hex(<span class="ty">U64</span>(u <span class="kw">shl</span> <span class="number">9</span>), <span class="number">2</span>); Out.ln; <span class="comment">(* 9 mod 8 = 1: 80 *)</span> |
| 233 | s := -<span class="number">64</span>; |
| 234 | Out.int(<span class="ty">I64</span>(s <span class="kw">shr</span> <span class="number">2</span>)); Out.char(<span class="string">' '</span>); <span class="comment">(* -16: the sign comes in *)</span> |
| 235 | Out.int(<span class="ty">I64</span>(s <span class="kw">shr</span> <span class="number">7</span>)); Out.ln; <span class="comment">(* -1 *)</span> |
| 236 | <span class="kw">end</span>; |
| 237 | |
| 238 | <span class="kw">end</span> Shifts.</code></pre> |
| 239 | <pre class="output"><code>80 0C 80 |
| 240 | -16 -1 |
| 241 | </code></pre> |
| 242 | <p>A packed value is the usual use of all this: several small numbers in one word. This puts a colour in the bytes of a <code>U32</code> and takes it out again.</p> |
| 243 | <div class="file"><span>Colour.raz</span></div><pre class="code"><code><span class="kw">module</span> Colour; |
| 244 | <span class="kw">import</span> Out; |
| 245 | |
| 246 | <span class="comment">(* red, green and blue, each 0 to 255, in the low 24 bits *)</span> |
| 247 | <span class="kw">proc</span> pack(r, g, b: <span class="ty">U8</span>): <span class="ty">U32</span>; |
| 248 | <span class="kw">begin</span> |
| 249 | <span class="kw">return</span> (<span class="ty">U32</span>(r) <span class="kw">shl</span> <span class="number">16</span>) | (<span class="ty">U32</span>(g) <span class="kw">shl</span> <span class="number">8</span>) | <span class="ty">U32</span>(b); |
| 250 | <span class="kw">end</span>; |
| 251 | |
| 252 | <span class="kw">proc</span> red(c: <span class="ty">U32</span>): <span class="ty">U8</span>; |
| 253 | <span class="kw">begin</span> |
| 254 | <span class="kw">return</span> <span class="ty">U8</span>(c <span class="kw">shr</span> <span class="number">16</span>); |
| 255 | <span class="kw">end</span>; |
| 256 | |
| 257 | <span class="kw">proc</span> green(c: <span class="ty">U32</span>): <span class="ty">U8</span>; |
| 258 | <span class="kw">begin</span> |
| 259 | <span class="kw">return</span> <span class="ty">U8</span>((c <span class="kw">shr</span> <span class="number">8</span>) & <span class="number">0FFH</span>); |
| 260 | <span class="kw">end</span>; |
| 261 | |
| 262 | <span class="kw">proc</span> blue(c: <span class="ty">U32</span>): <span class="ty">U8</span>; |
| 263 | <span class="kw">begin</span> |
| 264 | <span class="kw">return</span> <span class="ty">U8</span>(c & <span class="number">0FFH</span>); |
| 265 | <span class="kw">end</span>; |
| 266 | |
| 267 | <span class="kw">proc</span> main*; |
| 268 | <span class="kw">var</span> c: <span class="ty">U32</span>; |
| 269 | <span class="kw">begin</span> |
| 270 | c := pack(<span class="number">0FFH</span>, <span class="number">80H</span>, <span class="number">10H</span>); |
| 271 | Out.str(<span class="string">"0x"</span>); Out.hex(<span class="ty">U64</span>(c), <span class="number">6</span>); Out.ln; |
| 272 | Out.int(<span class="ty">I64</span>(red(c))); Out.char(<span class="string">' '</span>); |
| 273 | Out.int(<span class="ty">I64</span>(green(c))); Out.char(<span class="string">' '</span>); |
| 274 | Out.int(<span class="ty">I64</span>(blue(c))); Out.ln; |
| 275 | <span class="kw">end</span>; |
| 276 | |
| 277 | <span class="kw">end</span> Colour.</code></pre> |
| 278 | <pre class="output"><code>0xFF8010 |
| 279 | 255 128 16 |
| 280 | </code></pre> |
| 281 | <h2>Which one to use</h2> |
| 282 | <ul><li><strong><code>INT</code></strong> for lengths, indexes, counts, loop variables, and anything you'd write <code>int</code> for in C.</li><li><strong><code>U8</code></strong> for bytes, and for a string's elements (<code>[]U8</code>). <strong><code>I32</code></strong> for a character, a code point.</li><li><strong><code>U32</code>, <code>U64</code></strong> for hashes, bit sets, masks and addresses you do arithmetic on.</li><li><strong><code>I64</code></strong> for what must not shrink on a smaller target: a file's size or position, a time in nanoseconds, a number read from text.</li><li><strong><code>I8</code>, <code>I16</code>, <code>U16</code></strong> when a format or a layout says so, in a record or an array, to save space or match a protocol. Convert at the edge and compute in <code>INT</code>.</li><li><strong><code>UINT</code></strong> rarely: for a count that must use the whole address range, or an address.</li></ul><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="modules.html">← Modules</a><a href="index.html">Contents</a><a class="next" href="values.html">Values and conversions →</a></div></article></div> |
| 283 | </main> |
| 284 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 285 | </body> |
| 286 | </html> |