liveBook · Manning

Integer

As explained in Clojure: The Essential Reference

The int type is a fixed-width signed integer type representing whole-number values in the Java numeric type system.

An integer is a fixed-width, signed whole-number value. In Clojure, the primitive Java int type uses 32 bits and supports values from Integer/MIN_VALUE through Integer/MAX_VALUE. Integer arithmetic matters when hashing, image processing, coordinates, or other algorithms require predictable int-width behavior instead of Clojure’s usual long-based arithmetic. section 5.14 section 5.1

Use the unchecked-*-int family when an operation must cast inputs to int and return an int:

  • Use unchecked-add-int, unchecked-subtract-int, unchecked-multiply-int, unchecked-divide-int, or unchecked-remainder-int with two numeric arguments. section 5.14
  • Use unchecked-inc-int, unchecked-dec-int, or unchecked-negate-int with one numeric int argument. section 5.14
  • Expect overflow rather than promotion: crossing either 32-bit boundary wraps into the opposite sign. section 5.14 section 5.1

Returning an Integer

(unchecked-add-int 2.0 1) ;; 3 (type (unchecked-add-int 2.0 1)) ;; java.lang.Integer The operator casts its numeric arguments to primitive int and returns an Integer-valued result, unlike unchecked-add, which returns 3.0 for these inputs. section 5.14

Integer Overflow

(unchecked-add-int Integer/MAX_VALUE 1) ;; -2147483648 (unchecked-subtract-int Integer/MIN_VALUE 1) ;; 2147483647 These results illustrate two’s-complement rollover at the signed 32-bit boundaries. section 5.1

Integer Algorithms

A line-drawing routine uses unchecked integer subtraction, division, increment, decrement, and addition to calculate pixel coordinates and error values. It stores the result in a Java int array and paints selected coordinates with aset-int. section 5.14

Listing 5.11. → Integer math, Overflowing , More Use Cases
(unchecked-add-int [x y])
(unchecked-subtract-int [x y])
(unchecked-multiply-int [x y])
(unchecked-divide-int [x y])
(unchecked-inc-int [x])
(unchecked-dec-int [x])
(unchecked-negate-int [x])
(unchecked-remainder-int [x y])

Browse the complete unchecked-*-int operator family and its eight integer arithmetic operations. section 5.14

How Clojure: The Essential Reference covers it

How deep it goes
Defines 32-bit bounds, two’s-complement rollover, operator arities, casting, return types, and worked algorithmic uses.
Examples
Demonstrates typed addition, boundary rollover, line drawing, pixel coordinates, and Java int-array painting.
What it assumes
Assumes numeric arguments and teaches the 32-bit limits, primitive representation, operator arities, and rollover behavior.
What you can do after
Apply unchecked-*-int operators to implement fixed-width arithmetic and integer-based algorithms with explicit overflow behavior.
How it connects
Connects integer arithmetic with overflow, two’s complement, hashing algorithms, Java arrays, primitive types, and long arithmetic.

Also explained in

Compare how 15 books explain integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.