Stdlib.Nativeint(3)
π Stdlib.Nativeint
okay so bestie, welcome to the Stdlib.Nativeint page from the OCaml library manual (section 3, iykyk). It's giving "fixed-width integer module that's literally the size of your platform" and honestly? iconic. No cap, this one's a whole vibe β let's get into it. π¦
πΈ NAME
Stdlib.Nativeint β no description. Yeah, the original page just... doesn't describe it. Not me being lazy, that's canon. π Moving on.
π Module
Module Stdlib.Nativeintβ the module itself, your main character.Module Nativeint : (module Stdlib__Nativeint)β and here's what's actually inside it. π«Ά
β¨ Documentation
π§ the little constants
val zero : nativeintβ the native integer0. the origin story. βval one : nativeintβ the native integer1. yes queen.val minus_one : nativeintβ the native integer-1. the plot twist. π
π₯ arithmetic (the classics)
val neg : nativeint -> nativeintβ unary negation.val add : nativeint -> nativeint -> nativeintβ addition.val sub : nativeint -> nativeint -> nativeintβ subtraction.val mul : nativeint -> nativeint -> nativeintβ multiplication.val div : nativeint -> nativeint -> nativeintβ integer division. This one rounds the real quotient of its arguments toward zero, exactly as specified for(/). So like, it truncates. periodt.
β οΈ Raises Division_by_zero if the second argument is zero. do not do that. π
val unsigned_div : nativeint -> nativeint -> nativeintβ same asNativeint.div, except that arguments and result are interpreted as unsigned native integers. Since 4.08val rem : nativeint -> nativeint -> nativeintβ integer remainder. Ifyis not zero, the result ofNativeint.rem x ysatisfies these properties:Nativeint.zero <= Nativeint.rem x y < Nativeint.abs yandx = Nativeint.add (Nativeint.mul (Nativeint.div x y) y) (Nativeint.rem x y). Ify = 0,Nativeint.rem x yraisesDivision_by_zero. bestie, don't pass a zero, I'm begging. π«val unsigned_rem : nativeint -> nativeint -> nativeintβ same asNativeint.rem, except that arguments and result are interpreted as unsigned native integers. Since 4.08
π the fancy divisions (new gen)
val fdiv : nativeint -> nativeint -> nativeintβ floor division.fdiv x yis the real quotientx / yrounded down to an integer. We havefdiv x y <= div x y <= cdiv x yandcdiv x y - fdiv x y <= 1. Since 5.5
β οΈ RaisesDivision_by_zeroif the second argument is0.
val cdiv : nativeint -> nativeint -> nativeintβ ceil division.cdiv x yis the real quotientx / yrounded up to an integer. We havefdiv x y <= div x y <= cdiv x yandcdiv x y - fdiv x y <= 1. Since 5.5
β οΈ RaisesDivision_by_zeroif the second argument is0.
val ediv : nativeint -> nativeint -> nativeintβ Euclidean division.ediv x yis the real quotientx / yrounded down to an integer ify > 0and rounded up to an integer ify < 0. The remaindererem x y = x - ediv x y * yis always non-negative. Moreover,ediv x (-y) = - ediv x y. Since 5.5
β οΈ RaisesDivision_by_zeroif the second argument is0.
val erem : nativeint -> nativeint -> nativeintβ Euclidean remainder. Ifyis not zero, we havex = ediv x y * y + erem x yand0 <= erem x y <= abs y - 1. The result oferem x yis always non-negative, unlike the result ofrem x y, which has the sign ofx. Since 5.5
β οΈ RaisesDivision_by_zeroif the second argument is0.
π stepping + bounds
val succ : nativeint -> nativeintβ successor.Nativeint.succ xisNativeint.add x Nativeint.one.val pred : nativeint -> nativeintβ predecessor.Nativeint.pred xisNativeint.sub x Nativeint.one.val abs : nativeint -> nativeintβabs xis the absolute value ofx. Onmin_intthis ismin_intitself and thus remains negative. yeah it's a little cursed, that's just how it be. πval size : intβ the size in bits of a native integer. This is equal to32on a 32-bit platform and to64on a 64-bit platform. platform-dependent queen πval max_int : nativeintβ the greatest representable native integer, either2^31 - 1on a 32-bit platform, or2^63 - 1on a 64-bit platform.val min_int : nativeintβ the smallest representable native integer, either-2^31on a 32-bit platform, or-2^63on a 64-bit platform.
π¦ bit twiddling (my beloved)
val logand : nativeint -> nativeint -> nativeintβ bitwise logical and.val logor : nativeint -> nativeint -> nativeintβ bitwise logical or.val logxor : nativeint -> nativeint -> nativeintβ bitwise logical exclusive or.val lognot : nativeint -> nativeintβ bitwise logical negation.val shift_left : nativeint -> int -> nativeintβNativeint.shift_left x yshiftsxto the left byybits.
β οΈ The result is unspecified ify < 0ory >= bitsize, wherebitsizeis32on a 32-bit platform and64on a 64-bit platform. unspecified means we don't know and neither should you, bestie. π
val shift_right : nativeint -> int -> nativeintβNativeint.shift_right x yshiftsxto the right byybits. This is an arithmetic shift: the sign bit ofxis replicated and inserted in the vacated bits.
β οΈ The result is unspecified ify < 0ory >= bitsize.
val shift_right_logical : nativeint -> int -> nativeintβNativeint.shift_right_logical x yshiftsxto the right byybits. This is a logical shift: zeroes are inserted in the vacated bits regardless of the sign ofx.
β οΈ The result is unspecified ify < 0ory >= bitsize.
π conversions
val of_int : int -> nativeintβ convert the given integer (typeint) to a native integer (typenativeint).val to_int : nativeint -> intβ convert the given native integer (typenativeint) to an integer (typeint). The high-order bit is lost during the conversion. rip that bit πͺ¦val unsigned_to_int : nativeint -> int optionβ same asNativeint.to_int, but interprets the argument as an unsigned integer. ReturnsNoneif the unsigned value of the argument cannot fit into anint. Since 4.08val of_float : float -> nativeintβ convert the given floating-point number to a native integer, discarding the fractional part (truncate towards0).
β οΈ If the truncated floating-point number is outside the range [ Nativeint.min_int , Nativeint.max_int ], no exception is raised, and an unspecified, platform-dependent integer is returned. so like. good luck. π
val to_float : nativeint -> floatβ convert the given native integer to a floating-point number.val of_int32 : int32 -> nativeintβ convert the given 32-bit integer (typeint32) to a native integer.val to_int32 : nativeint -> int32β convert the given native integer to a 32-bit integer (typeint32). On 64-bit platforms, the 64-bit native integer is taken modulo2^32, i.e. the top 32 bits are lost. On 32-bit platforms, the conversion is exact. slay for the 32-bit girlies β¨
π strings
val of_string : string -> nativeintβ convert the given string to a native integer. The string is read in decimal (by default, or if the string begins with0u) or in hexadecimal, octal or binary if the string begins with0x,0oor0brespectively.
The 0u prefix reads the input as an unsigned integer in the range [0, 2*Nativeint.max_int+1]. If the input exceeds Nativeint.max_int it is converted to the signed integer Int64.min_int + input - Nativeint.max_int - 1.
β οΈ RaisesFailureif the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in typenativeint.
val of_string_opt : string -> nativeint optionβ same asof_string, but returnNoneinstead of raising. the chill version π«Ά Since 4.05val to_string : nativeint -> stringβ return the string representation of its argument, in decimal.
π the type + comparisons
type t = nativeintβ an alias for the type of native integers.val compare : t -> t -> intβ the comparison function for native integers, with the same specification ascompare. Along with the typet, this functioncompareallows the moduleNativeintto be passed as argument to the functorsSet.MakeandMap.Make. functor-ready, fr fr.val unsigned_compare : t -> t -> intβ same asNativeint.compare, except that arguments are interpreted as unsigned native integers. Since 4.08val equal : t -> t -> boolβ theequalfunction for native ints. Since 4.03val min : t -> t -> tβ return the smaller of the two arguments. Since 4.13val max : t -> t -> tβ return the greater of the two arguments. Since 4.13
β bit counting (the 5.5 glow-up)
val popcount : t -> intβ population count, also known as Hamming weight.popcount nis the number of1bits in the binary representation ofn. Negativenare represented in two's complement. Since 5.5val unsigned_bitsize : t -> intβunsigned_bitsize nis the minimal number of bits needed to representnas an unsigned binary number. It is the smallest integeribetween0andsizeinclusive such that0 <= n < 2{^i}(unsigned). Since 5.5val signed_bitsize : t -> intβsigned_bitsize nis the minimal number of bits needed to representnas a signed, two's complement binary number. It is the smallest integeribetween1andsizeinclusive such that-2{^i-1} <= n < 2{^i-1}(signed). Since 5.5val leading_zeros : t -> intβleading_zeros nis the number of leading (most significant)0bits in the binary representation ofn. It is an integer between0and64inclusive. Ifnis negative,leading_zeros n = 0since the most significant bit ofnis1.leading_zeros n = sizeif and only ifn = zero. Note thatleading_zeros n + unsigned_bitsize n = size. Since 5.5val leading_sign_bits : t -> intβleading_sign_bits nis the number of leading (most significant) sign bits in the binary representation ofn, excluding the sign bit itself. It is an integer between0and63inclusive. For positiven, it is the number of leading zero bits minus one. For negativen, it is the number of leading one bits minus one. Note thatleading_sign_bits n + signed_bitsize n = size. Since 5.5val trailing_zeros : t -> intβtrailing_zeros nis the number of trailing (least significant)0bits in the binary representation ofn. It is an integer between0andsizeinclusive. It is the largest integeri <= sizesuch that2{^i}dividesnevenly. For example,trailing_zeros n = 0if and only ifnis odd, andtrailing_zeros n = sizeif and only ifn = zero. Since 5.5
π§ hashing
val seeded_hash : int -> t -> intβ a seeded hash function for native ints, with the same output value asHashtbl.seeded_hash. This function allows this module to be passed as argument to the functorHashtbl.MakeSeeded. Since 5.1val hash : t -> intβ an unseeded hash function for native ints, with the same output value asHashtbl.hash. This function allows this module to be passed as argument to the functorHashtbl.Make. Since 5.1
and that's the whole page, bestie β no details left behind, we ate and left zero crumbs π
This lil' beauty was brought to you by OCamldoc, dated 2026-06-22, and it lives at home in Stdlib.Nativeint(3). go forth and shift some bits responsibly πβ¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms