Int32(3)
π NAME π
Int32 β 32-bit integers, bestie. that's the whole vibe. β¨
(this is the Int32 page out of the OCaml library manual, and yes we're gonna read the whole thing together π«Ά)
π¦ Module π¦
okay so here's the actual module declaration, copy-pasted with love:
Module Int32
: sig end
β¨ Documentation β¨
so Int32 is a module and it's all about 32-bit integers, periodt. π
This module provides operations on the type int32 of signed 32-bit integers. And here's the gag that makes it iconic: unlike the built-in int type, int32 is guaranteed to be exactly 32-bit wide on all platforms. no cap, every single platform. π
All arithmetic operations over int32 are taken modulo 2^32. that's the rule, we respect it. π
Performance notice, bestie, listen up: values of typeint32occupy more memory space than values of typeint, and arithmetic operations onint32are generally slower than those onint. so useint32only when your app genuinely requires exact 32-bit arithmetic. don't be out here using it for the aesthetic, it's not worth it π
π the literals
literals for 32-bit integers are suffixed by l (that's a lowercase L, not a one, iykyk):
let zero: int32 = 0l
let one: int32 = 1l
let m_one: int32 = -1l
πΈ the values, one by one
val zero : int32β the 32-bit integer0. the blueprint. the origin story. π±val one : int32β the 32-bit integer1. we love a main character. πval minus_one : int32β the 32-bit integer-1. it's giving negative energy and we stan. πval neg : int32 -> int32β unary negation. flip the sign, flip the vibe. πval add : int32 -> int32 -> int32β addition, bestie. math ate. βval sub : int32 -> int32 -> int32β subtraction. βval mul : int32 -> int32 -> int32β multiplication. βοΈval div : int32 -> int32 -> int32β integer division. this division rounds the real quotient of its arguments towards zero, as specified for(/). RaisesDivision_by_zeroif the second argument is zero. π«val unsigned_div : int32 -> int32 -> int32β same asInt32.div, except that arguments and result are interpreted as unsigned 32-bit integers. (since 4.08 β¨)val rem : int32 -> int32 -> int32β integer remainder. ifyis not zero, the result ofInt32.rem x ysatisfies this property:
x = Int32.add (Int32.mul (Int32.div x y) y) (Int32.rem x y)
and if y = 0, then Int32.rem x y raises Division_by_zero. π«
val unsigned_rem : int32 -> int32 -> int32β same asInt32.rem, except that arguments and result are interpreted as unsigned 32-bit integers. (since 4.08 β¨)val fdiv : int32 -> int32 -> int32β 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 : int32 -> int32 -> int32β 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 : int32 -> int32 -> int32β 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 : int32 -> int32 -> int32β 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.val succ : int32 -> int32β successor.Int32.succ xisInt32.add x Int32.one. one step forward, always. πΆββοΈval pred : int32 -> int32β predecessor.Int32.pred xisInt32.sub x Int32.one. one step back. πΆval abs : int32 -> int32βabs xis the absolute value ofx. but onmin_intthis ismin_intitself and thus remains negative. yes it's a jump scare, we deal πval max_int : int32β the greatest representable 32-bit integer,2^31 - 1. the ceiling, literally. πval min_int : int32β the smallest representable 32-bit integer,-2^31. the floor, literally. π»val logand : int32 -> int32 -> int32β bitwise logical and.val logor : int32 -> int32 -> int32β bitwise logical or.val logxor : int32 -> int32 -> int32β bitwise logical exclusive or.val lognot : int32 -> int32β bitwise logical negation.val shift_left : int32 -> int -> int32βInt32.shift_left x yshiftsxto the left byybits. β οΈ the result is unspecified ify < 0ory >= 32. do not test this, it's not a personality trait.val shift_right : int32 -> int -> int32βInt32.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 >= 32.val shift_right_logical : int32 -> int -> int32βInt32.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 >= 32.val of_int : int -> int32β convert the given integer (typeint) to a 32-bit integer (typeint32). on 64-bit platforms, the argument is taken modulo2^32. π’val to_int : int32 -> intβ convert the given 32-bit integer (typeint32) to an integer (typeint). on 32-bit platforms, the 32-bit integer is taken modulo2^31, i.e. the high-order bit is lost during the conversion π. on 64-bit platforms, the conversion is exact. slay.val unsigned_to_int : int32 -> int optionβ same asInt32.to_int, but interprets the argument as an unsigned integer. returnsNoneif the unsigned value of the argument cannot fit into anint. (since 4.08 β¨)val of_float : float -> int32β convert the given floating-point number to a 32-bit integer, discarding the fractional part (truncate towards0). if the truncated floating-point number is outside the range[ Int32.min_int , Int32.max_int ], no exception is raised, and an unspecified, platform-dependent integer is returned. π so like... it's giving chaos but silently.val to_float : int32 -> floatβ convert the given 32-bit integer to a floating-point number.val of_string : string -> int32β convert the given string to a 32-bit 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. π’ the0uprefix reads the input as an unsigned integer in the range[0, 2*Int32.max_int+1]. if the input exceedsInt32.max_intit is converted to the signed integerInt32.min_int + input - Int32.max_int - 1. the_(underscore) character can appear anywhere in the string and is ignored. cute. RaisesFailureif the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in typeint32.val of_string_opt : string -> int32 optionβ same asof_string, but returnNoneinstead of raising. (since 4.05 β¨) no drama, justNone.val to_string : int32 -> stringβ return the string representation of its argument, in signed decimal.val bits_of_float : float -> int32β return the internal representation of the givenfloataccording to the IEEE 754 floating-point 'single format' bit layout. bit 31 of the result represents the sign of the float; bits 30 to 23 represent the (biased) exponent; bits 22 to 0 represent the mantissa. π¬val float_of_bits : int32 -> floatβ return the floating-point number whose internal representation, according to the IEEE 754 floating-point 'single format' bit layout, is the givenint32. the reverse glow-up ofbits_of_float. π¦type t = int32β an alias for the type of 32-bit integers. same energy, different name. π«val compare : t -> t -> intβ the comparison function for 32-bit integers, with the same specification ascompare. along with the typet, this functioncompareallows the moduleInt32to be passed as argument to the functorsSet.MakeandMap.Make. πval unsigned_compare : t -> t -> intβ same asInt32.compare, except that arguments are interpreted as unsigned 32-bit integers. (since 4.08 β¨)val equal : t -> t -> boolβ theequalfunction forint32s. (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 β¨)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.5 β¨)val unsigned_bitsize : t -> intβunsigned_bitsize nis the minimal number of bits needed to representnas an unsigned binary number. it is the smallest integeribetween0and32inclusive such that0 <= n < 2{^i}(unsigned). (since 5.5 β¨)val 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 integeribetween1and32inclusive such that-2{^i-1} <= n < 2{^i-1}(signed). (since 5.5 β¨)val leading_zeros : t -> intβleading_zeros nis the number of leading (most significant)0bits in the binary representation ofn. it is an integer between0and32inclusive. ifnis negative,leading_zeros n = 0since the most significant bit ofnis1.leading_zeros n = 32if and only ifn = zero. note thatleading_zeros n + unsigned_bitsize n = 32. (since 5.5 β¨)val 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 between0and31inclusive. 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 = 32. (since 5.5 β¨)val trailing_zeros : t -> intβtrailing_zeros nis the number of trailing (least significant)0bits in the binary representation ofn. it is an integer between0and32inclusive. it is the largest integeri <= 32such that2{^i}dividesnevenly. for example,trailing_zeros n = 0if and only ifnis odd, andtrailing_zeros n = 32if and only ifn = zero. (since 5.5 β¨)val seeded_hash : int -> t -> intβ a seeded hash function for 32-bit 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.1 β¨)val hash : t -> intβ an unseeded hash function for 32-bit 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, we read it, we understood it, we're basically 32-bit literate now π
π§ this cuteness was brought to you by OCamldoc, version dated 2026-06-22, and it's theInt32(3)page β ok bye, go write some0lliterals 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