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 integer 0. the origin story. ⭐
  • val one : nativeint β€” the native integer 1. 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 as Nativeint.div, except that arguments and result are interpreted as unsigned native integers. Since 4.08
  • val rem : nativeint -> nativeint -> nativeint β€” integer remainder. If y is not zero, the result of Nativeint.rem x y satisfies these properties: Nativeint.zero <= Nativeint.rem x y < Nativeint.abs y and x = Nativeint.add (Nativeint.mul (Nativeint.div x y) y) (Nativeint.rem x y). If y = 0, Nativeint.rem x y raises Division_by_zero. bestie, don't pass a zero, I'm begging. 🫠
  • val unsigned_rem : nativeint -> nativeint -> nativeint β€” same as Nativeint.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 y is the real quotient x / y rounded down to an integer. We have fdiv x y <= div x y <= cdiv x y and cdiv x y - fdiv x y <= 1. Since 5.5
⚠️ Raises Division_by_zero if the second argument is 0.
  • val cdiv : nativeint -> nativeint -> nativeint β€” ceil division. cdiv x y is the real quotient x / y rounded up to an integer. We have fdiv x y <= div x y <= cdiv x y and cdiv x y - fdiv x y <= 1. Since 5.5
⚠️ Raises Division_by_zero if the second argument is 0.
  • val ediv : nativeint -> nativeint -> nativeint β€” Euclidean division. ediv x y is the real quotient x / y rounded down to an integer if y > 0 and rounded up to an integer if y < 0. The remainder erem x y = x - ediv x y * y is always non-negative. Moreover, ediv x (-y) = - ediv x y. Since 5.5
⚠️ Raises Division_by_zero if the second argument is 0.
  • val erem : nativeint -> nativeint -> nativeint β€” Euclidean remainder. If y is not zero, we have x = ediv x y * y + erem x y and 0 <= erem x y <= abs y - 1. The result of erem x y is always non-negative, unlike the result of rem x y, which has the sign of x. Since 5.5
⚠️ Raises Division_by_zero if the second argument is 0.

πŸŽ€ stepping + bounds

  • val succ : nativeint -> nativeint β€” successor. Nativeint.succ x is Nativeint.add x Nativeint.one.
  • val pred : nativeint -> nativeint β€” predecessor. Nativeint.pred x is Nativeint.sub x Nativeint.one.
  • val abs : nativeint -> nativeint β€” abs x is the absolute value of x. On min_int this is min_int itself 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 to 32 on a 32-bit platform and to 64 on a 64-bit platform. platform-dependent queen πŸ’…
  • val max_int : nativeint β€” the greatest representable native integer, either 2^31 - 1 on a 32-bit platform, or 2^63 - 1 on a 64-bit platform.
  • val min_int : nativeint β€” the smallest representable native integer, either -2^31 on a 32-bit platform, or -2^63 on 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 y shifts x to the left by y bits.
⚠️ The result is unspecified if y < 0 or y >= bitsize, where bitsize is 32 on a 32-bit platform and 64 on 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 y shifts x to the right by y bits. This is an arithmetic shift: the sign bit of x is replicated and inserted in the vacated bits.
⚠️ The result is unspecified if y < 0 or y >= bitsize.
  • val shift_right_logical : nativeint -> int -> nativeint β€” Nativeint.shift_right_logical x y shifts x to the right by y bits. This is a logical shift: zeroes are inserted in the vacated bits regardless of the sign of x.
⚠️ The result is unspecified if y < 0 or y >= bitsize.

πŸ’œ conversions

  • val of_int : int -> nativeint β€” convert the given integer (type int) to a native integer (type nativeint).
  • val to_int : nativeint -> int β€” convert the given native integer (type nativeint) to an integer (type int). The high-order bit is lost during the conversion. rip that bit πŸͺ¦
  • val unsigned_to_int : nativeint -> int option β€” same as Nativeint.to_int, but interprets the argument as an unsigned integer. Returns None if the unsigned value of the argument cannot fit into an int. Since 4.08
  • val of_float : float -> nativeint β€” convert the given floating-point number to a native integer, discarding the fractional part (truncate towards 0).
⚠️ 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 (type int32) to a native integer.
  • val to_int32 : nativeint -> int32 β€” convert the given native integer to a 32-bit integer (type int32). On 64-bit platforms, the 64-bit native integer is taken modulo 2^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 with 0u) or in hexadecimal, octal or binary if the string begins with 0x, 0o or 0b respectively.

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.

⚠️ Raises Failure if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type nativeint.
  • val of_string_opt : string -> nativeint option β€” same as of_string, but return None instead of raising. the chill version 🫢 Since 4.05
  • val 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 as compare. Along with the type t, this function compare allows the module Nativeint to be passed as argument to the functors Set.Make and Map.Make. functor-ready, fr fr.
  • val unsigned_compare : t -> t -> int β€” same as Nativeint.compare, except that arguments are interpreted as unsigned native integers. Since 4.08
  • val equal : t -> t -> bool β€” the equal function for native ints. Since 4.03
  • val min : t -> t -> t β€” return the smaller of the two arguments. Since 4.13
  • val 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 n is the number of 1 bits in the binary representation of n. Negative n are represented in two's complement. Since 5.5
  • val unsigned_bitsize : t -> int β€” unsigned_bitsize n is the minimal number of bits needed to represent n as an unsigned binary number. It is the smallest integer i between 0 and size inclusive such that 0 <= n < 2{^i} (unsigned). Since 5.5
  • val signed_bitsize : t -> int β€” signed_bitsize n is the minimal number of bits needed to represent n as a signed, two's complement binary number. It is the smallest integer i between 1 and size inclusive such that -2{^i-1} <= n < 2{^i-1} (signed). Since 5.5
  • val leading_zeros : t -> int β€” leading_zeros n is the number of leading (most significant) 0 bits in the binary representation of n. It is an integer between 0 and 64 inclusive. If n is negative, leading_zeros n = 0 since the most significant bit of n is 1. leading_zeros n = size if and only if n = zero. Note that leading_zeros n + unsigned_bitsize n = size. Since 5.5
  • val leading_sign_bits : t -> int β€” leading_sign_bits n is the number of leading (most significant) sign bits in the binary representation of n, excluding the sign bit itself. It is an integer between 0 and 63 inclusive. For positive n, it is the number of leading zero bits minus one. For negative n, it is the number of leading one bits minus one. Note that leading_sign_bits n + signed_bitsize n = size. Since 5.5
  • val trailing_zeros : t -> int β€” trailing_zeros n is the number of trailing (least significant) 0 bits in the binary representation of n. It is an integer between 0 and size inclusive. It is the largest integer i <= size such that 2{^i} divides n evenly. For example, trailing_zeros n = 0 if and only if n is odd, and trailing_zeros n = size if and only if n = zero. Since 5.5

🧚 hashing

  • val seeded_hash : int -> t -> int β€” a seeded hash function for native ints, with the same output value as Hashtbl.seeded_hash. This function allows this module to be passed as argument to the functor Hashtbl.MakeSeeded. Since 5.1
  • val hash : t -> int β€” an unseeded hash function for native ints, with the same output value as Hashtbl.hash. This function allows this module to be passed as argument to the functor Hashtbl.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