<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="API documentation for the Rust `PrimInt` trait in crate `num_traits`."><metaname="keywords"content="rust, rustlang, rust-lang, PrimInt"><title>num_traits::PrimInt - Rust</title><linkrel="stylesheet"type="text/css"href="../normalize.css"><linkrel="stylesheet"type="text/css"href="../rustdoc.css"id="mainThemeStyle"><linkrel="stylesheet"type="text/css"href="../dark.css"><linkrel="stylesheet"type="text/css"href="../light.css"id="themeStyle"><scriptsrc="../storage.js"></script><noscript><linkrel="stylesheet"href="../noscript.css"></noscript><linkrel="shortcut icon"href="../favicon.ico"><styletype="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><bodyclass="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="sidebar"><divclass="sidebar-menu">☰</div><ahref='../num_traits/index.html'><divclass='logo-container'><imgsrc='../rust-logo.png'alt='logo'></div></a><pclass='location'>Trait PrimInt</p><divclass="sidebar-elems"><divclass="block items"><aclass="sidebar-title"href="#required-methods">Required Methods</a><divclass="sidebar-links"><ahref="#tymethod.count_ones">count_ones</a><ahref="#tymethod.count_zeros">count_zeros</a><ahref="#tymethod.from_be">from_be</a><ahref="#tymethod.from_le">from_le</a><ahref="#tymethod.leading_zeros">leading_zeros</a><ahref="#tymethod.pow">pow</a><ahref="#tymethod.rotate_left">rotate_left</a><ahref="#tymethod.rotate_right">rotate_right</a><ahref="#tymethod.signed_shl">signed_shl</a><ahref="#tymethod.signed_shr">signed_shr</a><ahref="#tymethod.swap_bytes">swap_bytes</a><ahref="#tymethod.to_be">to_be</a><ahref="#tymethod.to_le">to_le</a><ahref="#tymethod.trailing_zeros">trailing_zeros</a><ahref="#tymethod.unsigned_shl">unsigned_shl</a><ahref="#tymethod.unsigned_shr">unsigned_shr</a></div><aclass="sidebar-title"href="#foreign-impls">Implementations on Foreign Types</a><divclass="sidebar-links"><ahref="#impl-PrimInt-for-i128">i128</a><ahref="#impl-PrimInt-for-i16">i16</a><ahref="#impl-PrimInt-for-i32">i32</a><ahref="#impl-PrimInt-for-i64">i64</a><ahref="#impl-PrimInt-for-i8">i8</a><ahref="#impl-PrimInt-for-isize">isize</a><ahref="#impl-PrimInt-for-u128">u128</a><ahref="#impl-PrimInt-for-u16">u16</a><ahref="#impl-PrimInt-for-u32">u32</a><ahref="#impl-PrimInt-for-u64">u64</a><ahref="#impl-PrimInt-for-u8">u8</a><ahref="#impl-PrimInt-for-usize">usize</a></div><aclass="sidebar-title"href="#implementors">Implementors</a></div><pclass='location'><ahref='index.html'>num_traits</a></p><script>window.sidebarCurrent={name:'PrimInt',ty:'trait',relpath:''};</script><scriptdefersrc="sidebar-items.js"></script></div></nav><divclass="theme-picker"><buttonid="theme-picker"aria-label="Pick another theme!"><imgsrc="../brush.svg"width="18"alt="Pick another theme!"></button><divid="theme-choices"></div></div><scriptsrc="../theme.js"></script><navclass="sub"><formclass="search-form js-only"><divclass="search-container"><div><selectid="crate-search"><optionvalue="All crates">All crates</option></select><inputclass="search-input"name="search"autocomplete="off"spellcheck="false"placeholder="Click or press ‘S’ to search, ‘?’ for more options…"type="search"></div><aid="settings-menu"href="../settings.html"><imgsrc="../wheel.svg"width="18"alt="Change settings"></a></div></form></nav><sectionid="main"class="content"><h1class='fqn'><spanclass='out-of-band'><spanid='render-detail'><aid="toggle-all-docs"href="javascript:void(0)"title="collapse all docs">[<spanclass='inner'>−</span>]</a></span><aclass='srclink'href='../src/num_traits/int.rs.html#34-307'title='goto source code'>[src]</a></span><spanclass='in-band'>Trait <ahref='index.html'>num_traits</a>::<wbr><ac
}</pre></div><divclass='docblock'><p>Generic trait for primitive integers.</p>
<p>The <code>PrimInt</code> trait is an abstraction over the builtin primitive integer types (e.g., <code>u8</code>,
<code>u32</code>, <code>isize</code>, <code>i128</code>, ...). It inherits the basic numeric traits and extends them with
bitwise operators and non-wrapping arithmetic.</p>
<p>The trait explicitly inherits <code>Copy</code>, <code>Eq</code>, <code>Ord</code>, and <code>Sized</code>. The intention is that all
types implementing this trait behave like primitive types that are passed by value by default
and behave like builtin integers. Furthermore, the types are expected to expose the integer
value in binary representation and support bitwise operators. The standard bitwise operations
(e.g., bitwise-and, bitwise-or, right-shift, left-shift) are inherited and the trait extends
these with introspective queries (e.g., <code>PrimInt::count_ones()</code>, <code>PrimInt::leading_zeros()</code>),
bitwise combinators (e.g., <code>PrimInt::rotate_left()</code>), and endianness converters (e.g.,
<code>PrimInt::to_be()</code>).</p>
<p>All <code>PrimInt</code> types are expected to be fixed-width binary integers. The width can be queried
via <code>T::zero().count_zeros()</code>. The trait currently lacks a way to query the width at
compile-time.</p>
<p>While a default implementation for all builtin primitive integers is provided, the trait is in
no way restricted to these. Other integer types that fulfil the requirements are free to
implement the trait was well.</p>
<p>This trait and many of the method names originate in the unstable <code>core::num::Int</code> trait from
the rust standard library. The original trait was never stabilized and thus removed from the
standard library.</p>
</div>
<h2id='required-methods'class='small-section-header'>Required methods<ahref='#required-methods'class='anchor'></a></h2><divclass='methods'><h3id='tymethod.count_ones'class='method'><codeid='count_ones.v'>fn <ahref='#tymethod.count_ones'class='fnname'>count_ones</a>(self) -><aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><divclass='docblock'><p>Returns the number of ones in the binary representation of <code>self</code>.</p>
</div><h3id='tymethod.count_zeros'class='method'><codeid='count_zeros.v'>fn <ahref='#tymethod.count_zeros'class='fnname'>count_zeros</a>(self) -><aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><divclass='docblock'><p>Returns the number of zeros in the binary representation of <code>self</code>.</p>
</div><h3id='tymethod.leading_zeros'class='method'><codeid='leading_zeros.v'>fn <ahref='#tymethod.leading_zeros'class='fnname'>leading_zeros</a>(self) -><aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><divclass='docblock'><p>Returns the number of leading zeros in the binary representation
</div><h3id='tymethod.trailing_zeros'class='method'><codeid='trailing_zeros.v'>fn <ahref='#tymethod.trailing_zeros'class='fnname'>trailing_zeros</a>(self) -><aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><divclass='docblock'><p>Returns the number of trailing zeros in the binary representation
</div><h3id='tymethod.rotate_left'class='method'><codeid='rotate_left.v'>fn <ahref='#tymethod.rotate_left'class='fnname'>rotate_left</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, wrapping
the truncated bits to the end of the resulting integer.</p>
</div><h3id='tymethod.rotate_right'class='method'><codeid='rotate_right.v'>fn <ahref='#tymethod.rotate_right'class='fnname'>rotate_right</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, wrapping
the truncated bits to the beginning of the resulting integer.</p>
</div><h3id='tymethod.signed_shl'class='method'><codeid='signed_shl.v'>fn <ahref='#tymethod.signed_shl'class='fnname'>signed_shl</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, filling
zeros in the least significant bits.</p>
<p>This is bitwise equivalent to signed <code>Shl</code>.</p>
</div><h3id='tymethod.signed_shr'class='method'><codeid='signed_shr.v'>fn <ahref='#tymethod.signed_shr'class='fnname'>signed_shr</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, copying
the "sign bit" in the most significant bits even for unsigned types.</p>
<p>This is bitwise equivalent to signed <code>Shr</code>.</p>
</div><h3id='tymethod.unsigned_shl'class='method'><codeid='unsigned_shl.v'>fn <ahref='#tymethod.unsigned_shl'class='fnname'>unsigned_shl</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, filling
zeros in the least significant bits.</p>
<p>This is bitwise equivalent to unsigned <code>Shl</code>.</p>
</div><h3id='tymethod.unsigned_shr'class='method'><codeid='unsigned_shr.v'>fn <ahref='#tymethod.unsigned_shr'class='fnname'>unsigned_shr</a>(self, n: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, filling
zeros in the most significant bits.</p>
<p>This is bitwise equivalent to unsigned <code>Shr</code>.</p>
</div><h3id='tymethod.swap_bytes'class='method'><codeid='swap_bytes.v'>fn <ahref='#tymethod.swap_bytes'class='fnname'>swap_bytes</a>(self) -> Self</code></h3><divclass='docblock'><p>Reverses the byte order of the integer.</p>
</div><h3id='tymethod.from_be'class='method'><codeid='from_be.v'>fn <ahref='#tymethod.from_be'class='fnname'>from_be</a>(x: Self) -> Self</code></h3><divclass='docblock'><p>Convert an integer from big endian to the target's endianness.</p>
<p>On big endian this is a no-op. On little endian the bytes are swapped.</p>
</div><h3id='tymethod.from_le'class='method'><codeid='from_le.v'>fn <ahref='#tymethod.from_le'class='fnname'>from_le</a>(x: Self) -> Self</code></h3><divclass='docblock'><p>Convert an integer from little endian to the target's endianness.</p>
<p>On little endian this is a no-op. On big endian the bytes are swapped.</p>
</div><h3id='tymethod.to_be'class='method'><codeid='to_be.v'>fn <ahref='#tymethod.to_be'class='fnname'>to_be</a>(self) -> Self</code></h3><divclass='docblock'><p>Convert <code>self</code> to big endian from the target's endianness.</p>
<p>On big endian this is a no-op. On little endian the bytes are swapped.</p>
</div><h3id='tymethod.to_le'class='method'><codeid='to_le.v'>fn <ahref='#tymethod.to_le'class='fnname'>to_le</a>(self) -> Self</code></h3><divclass='docblock'><p>Convert <code>self</code> to little endian from the target's endianness.</p>
<p>On little endian this is a no-op. On big endian the bytes are swapped.</p>
</div><h3id='tymethod.pow'class='method'><codeid='pow.v'>fn <ahref='#tymethod.pow'class='fnname'>pow</a>(self, exp: <aclass="primitive"href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><divclass='docblock'><p>Raises self to the power of <code>exp</code>, using exponentiation by squaring.</p>
</script></section><sectionid="search"class="content hidden"></section><sectionclass="footer"></section><asideid="help"class="hidden"><div><h1class="hidden">Help</h1><divclass="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><divclass="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath="../";window.currentCrate="num_traits";</script><scriptsrc="../aliases.js"></script><scriptsrc="../main.js"></script><scriptdefersrc="../search-index.js"></script></body></html>