.TH "Int" 3 2026-06-22 OCamldoc "OCaml library" .SH NAME Int \- Integer values. .SH Module Module Int .SH Documentation .sp Module .BI "Int" : .B sig end .sp Integer values\&. .sp Integers are .ft B Sys\&.int_size .ft R bits wide and use two\&'s complement representation\&. All operations are taken modulo 2^ .ft B Sys\&.int_size .ft R \&. They do not fail on overflow\&. .sp .B "Since" 4.08 .sp .sp .sp .PP .SS Integers .PP .I type t = .B int .sp The type for integer values\&. .sp .I val zero : .B int .sp .ft B zero .ft R is the integer .ft B 0 .ft R \&. .sp .I val one : .B int .sp .ft B one .ft R is the integer .ft B 1 .ft R \&. .sp .I val minus_one : .B int .sp .ft B minus_one .ft R is the integer .ft B \-1 .ft R \&. .sp .I val neg : .B int -> int .sp .ft B neg x .ft R is .ft B ~\-x .ft R \&. .sp .I val add : .B int -> int -> int .sp .ft B add x y .ft R is the addition .ft B x + y .ft R \&. .sp .I val sub : .B int -> int -> int .sp .ft B sub x y .ft R is the subtraction .ft B x \- y .ft R \&. .sp .I val mul : .B int -> int -> int .sp .ft B mul x y .ft R is the multiplication .ft B x * y .ft R \&. .sp .I val div : .B int -> int -> int .sp Rounding division\&. .ft B div x y .ft R is the real quotient .ft B x / y .ft R rounded towards zero to an integer\&. See .ft B (/) .ft R for details\&. .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val rem : .B int -> int -> int .sp .ft B rem x y .ft R is the remainder of the rounding division .ft B div x y .ft R \&. We have .ft B rem x y = x \- div x y * y .ft R \&. See .ft B (mod) .ft R for details\&. .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val fdiv : .B int -> int -> int .sp Floor division\&. .ft B fdiv x y .ft R is the real quotient .ft B x / y .ft R rounded down to an integer\&. We have .ft B fdiv x y <= div x y <= cdiv x y .ft R and .ft B cdiv x y \- fdiv x y <= 1 .ft R \&. .sp .B "Since" 5.5 .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val cdiv : .B int -> int -> int .sp Ceil division\&. .ft B cdiv x y .ft R is the real quotient .ft B x / y .ft R rounded up to an integer\&. We have .ft B fdiv x y <= div x y <= cdiv x y .ft R and .ft B cdiv x y \- fdiv x y <= 1 .ft R \&. .sp .B "Since" 5.5 .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val ediv : .B int -> int -> int .sp Euclidean division\&. .ft B ediv x y .ft R is the real quotient .ft B x / y .ft R rounded down to an integer if .ft B y > 0 .ft R and rounded up to an integer if .ft B y < 0 .ft R \&. The remainder .ft B erem x y = x \- ediv x y * y .ft R is always non\-negative\&. Moreover, .ft B ediv x (\-y) .ft R = .ft B \- ediv x y .ft R \&. .sp .B "Since" 5.5 .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val erem : .B int -> int -> int .sp Euclidean remainder\&. If .ft B y .ft R is not zero, we have .ft B x = ediv x y * y + erem x y .ft R and .ft B 0 <= erem x y <= abs y \- 1 .ft R \&. The result of .ft B erem x y .ft R is always non\-negative, unlike the result of .ft B rem x y .ft R , which has the sign of .ft B x .ft R \&. .sp .B "Since" 5.5 .sp .B "Raises Division_by_zero" if the second argument is 0\&. .sp .I val succ : .B int -> int .sp .ft B succ x .ft R is .ft B add x 1 .ft R \&. .sp .I val pred : .B int -> int .sp .ft B pred x .ft R is .ft B sub x 1 .ft R \&. .sp .I val abs : .B int -> int .sp .ft B abs x .ft R is the absolute value of .ft B x .ft R \&. That is .ft B x .ft R if .ft B x .ft R is positive and .ft B neg x .ft R if .ft B x .ft R is negative\&. Warning\&. This may be negative if the argument is .ft B Int\&.min_int .ft R \&. .sp .I val max_int : .B int .sp .ft B max_int .ft R is the greatest representable integer, .ft B 2 .ft R ^ .ft B Sys\&.int_size \- 1 .ft R .ft B \-1 .ft R \&. .sp .I val min_int : .B int .sp .ft B min_int .ft R is the smallest representable integer, .ft B \-2 .ft R ^ .ft B Sys\&.int_size \- 1 .ft R \&. .sp .I val logand : .B int -> int -> int .sp .ft B logand x y .ft R is the bitwise logical and of .ft B x .ft R and .ft B y .ft R \&. .sp .I val logor : .B int -> int -> int .sp .ft B logor x y .ft R is the bitwise logical or of .ft B x .ft R and .ft B y .ft R \&. .sp .I val logxor : .B int -> int -> int .sp .ft B logxor x y .ft R is the bitwise logical exclusive or of .ft B x .ft R and .ft B y .ft R \&. .sp .I val lognot : .B int -> int .sp .ft B lognot x .ft R is the bitwise logical negation of .ft B x .ft R \&. .sp .I val shift_left : .B int -> int -> int .sp .ft B shift_left x n .ft R shifts .ft B x .ft R to the left by .ft B n .ft R bits\&. The result is unspecified if .ft B n < 0 .ft R or .ft B n > .ft R .ft B Sys\&.int_size .ft R \&. .sp .I val shift_right : .B int -> int -> int .sp .ft B shift_right x n .ft R shifts .ft B x .ft R to the right by .ft B n .ft R bits\&. This is an arithmetic shift: the sign bit of .ft B x .ft R is replicated and inserted in the vacated bits\&. The result is unspecified if .ft B n < 0 .ft R or .ft B n > .ft R .ft B Sys\&.int_size .ft R \&. .sp .I val shift_right_logical : .B int -> int -> int .sp .ft B shift_right_logical x n .ft R shifts .ft B x .ft R to the right by .ft B n .ft R bits\&. This is a logical shift: zeroes are inserted in the vacated bits regardless of the sign of .ft B x .ft R \&. The result is unspecified if .ft B n < 0 .ft R or .ft B n > .ft R .ft B Sys\&.int_size .ft R \&. .sp .PP .SS Predicates and comparisons .PP .I val equal : .B int -> int -> bool .sp .ft B equal x y .ft R is .ft B true .ft R if and only if .ft B x = y .ft R \&. .sp .I val compare : .B int -> int -> int .sp .ft B compare x y .ft R is .ft B compare .ft R .ft B x y .ft R but more efficient\&. .sp .I val min : .B int -> int -> int .sp Return the smaller of the two arguments\&. .sp .B "Since" 4.13 .sp .I val max : .B int -> int -> int .sp Return the greater of the two arguments\&. .sp .B "Since" 4.13 .sp .PP .SS Bit counting .PP .I val popcount : .B t -> int .sp Population count, also known as Hamming weight\&. .ft B popcount n .ft R is the number of 1 bits in the binary representation of .ft B n .ft R \&. Negative .ft B n .ft R are represented in two\&'s complement\&. .sp .B "Since" 5.5 .sp .I val unsigned_bitsize : .B t -> int .sp .ft B unsigned_bitsize n .ft R is the minimal number of bits needed to represent .ft B n .ft R as an unsigned binary number\&. It is the smallest integer .ft B i .ft R between 0 and .ft B Sys\&.int_size .ft R inclusive such that .ft B 0 <= n < 2{^i} .ft R (unsigned)\&. .sp .B "Since" 5.5 .sp .I val signed_bitsize : .B t -> int .sp .ft B signed_bitsize n .ft R is the minimal number of bits needed to represent .ft B n .ft R as a signed, two\&'s complement binary number\&. It is the smallest integer .ft B i .ft R between 1 and .ft B Sys\&.int_size .ft R inclusive such that .ft B \-2{^i\-1} <= n < 2{^i\-1} .ft R (signed)\&. .sp .B "Since" 5.5 .sp .I val leading_zeros : .B t -> int .sp .ft B leading_zeros n .ft R is the number of leading (most significant) 0 bits in the binary representation of .ft B n .ft R \&. It is an integer between 0 and .ft B Sys\&.int_size .ft R inclusive\&. If .ft B n .ft R is negative, .ft B leading_zeros n = 0 .ft R since the most significant bit of .ft B n .ft R is 1\&. .ft B leading_zeros n = {!Sys\&.int_size} .ft R if and only if .ft B n = zero .ft R \&. Note that .ft B leading_zeros n + unsigned_bitsize n = {!Sys\&.int_size} .ft R \&. .sp .B "Since" 5.5 .sp .I val leading_sign_bits : .B t -> int .sp .ft B leading_sign_bits n .ft R is the number of leading (most significant) sign bits in the binary representation of .ft B n .ft R , excluding the sign bit itself\&. It is an integer between 0 and .ft B {!Sys\&.int_size} \- 1 .ft R inclusive\&. For positive .ft B n .ft R , it is the number of leading zero bits minus one\&. For negative .ft B n .ft R , it is the number of leading one bits minus one\&. Note that .ft B leading_sign_bits n + signed_bitsize n = {!Sys\&.int_size} .ft R \&. .sp .B "Since" 5.5 .sp .I val trailing_zeros : .B t -> int .sp .ft B trailing_zeros n .ft R is the number of trailing (least significant) 0 bits in the binary representation of .ft B n .ft R \&. It is an integer between 0 and .ft B Sys\&.int_size .ft R inclusive\&. It is the largest integer .ft B i <= {!Sys\&.int_size} .ft R such that .ft B 2{^i} .ft R divides .ft B n .ft R evenly\&. For example, .ft B trailing_zeros n = 0 .ft R if and only if .ft B n .ft R is odd, and .ft B trailing_zeros n = {!Sys\&.int_size} .ft R if and only if .ft B n = zero .ft R \&. .sp .B "Since" 5.5 .sp .PP .SS Converting .PP .I val to_float : .B int -> float .sp .ft B to_float x .ft R is .ft B x .ft R as a floating point number\&. .sp .I val of_float : .B float -> int .sp .ft B of_float x .ft R truncates .ft B x .ft R to an integer\&. The result is unspecified if the argument is .ft B nan .ft R or falls outside the range of representable integers\&. .sp .I val to_string : .B int -> string .sp .ft B to_string x .ft R is the written representation of .ft B x .ft R in decimal\&. .sp .I val seeded_hash : .B int -> int -> int .sp A seeded hash function for ints, with the same output value as .ft B Hashtbl\&.seeded_hash .ft R \&. This function allows this module to be passed as argument to the functor .ft B Hashtbl\&.MakeSeeded .ft R \&. .sp .B "Since" 5.1 .sp .I val hash : .B int -> int .sp An unseeded hash function for ints, with the same output value as .ft B Hashtbl\&.hash .ft R \&. This function allows this module to be passed as argument to the functor .ft B Hashtbl\&.Make .ft R \&. .sp .B "Since" 5.1 .sp