{"id":22595,"date":"2018-08-31T15:17:26","date_gmt":"2018-08-31T19:17:26","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=22595"},"modified":"2025-08-17T14:57:23","modified_gmt":"2025-08-17T18:57:23","slug":"adding-ismultipleof-to-swifts-binaryinteger","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/08\/31\/adding-ismultipleof-to-swifts-binaryinteger\/","title":{"rendered":"Adding isMultiple(of:) to Swift&rsquo;s BinaryInteger"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/accepted-with-modifications-se-0225-adding-iseven-isodd-ismultiple-to-binaryinteger\/15689\">John McCall<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/accepted-with-modifications-se-0225-adding-iseven-isodd-ismultiple-to-binaryinteger\/15689\"><p>It is substantially more fluent than its standard implementation of <code>value % divisor == 0<\/code>.  Divisibility testing is the operation people actually want; the <code>%<\/code> operator is just the way they have to achieve it.  <code>%<\/code> is rarely used outside of this idiom, and many programmers have no other reason to know it.  It is not a familiar operator for new programmers especially: they will usually be comfortable with remainders in the context of division, but they aren&rsquo;t used to thinking about computing a remainder as a separate operation, and they certainly don&rsquo;t recognize <code>%<\/code> as a symbol for it.  Even experienced programmers often mentally &ldquo;pattern-match&rdquo; this sort of expression to recognize it as a divisibility test instead of thinking through the arithmetic of it.<\/p><p>Encouraging the use of <code>isMultiple(of:)<\/code> (and <code>!isMultiple(of:)<\/code>) serves to counter bugs around negative remainders.<\/p><p>It has some potential for better performance, especially when applied to large integers.  In this case, this impact would probably not be a sufficient justification on its own.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/duriansoftware.com\/joe\/Optimizing-is-multiple-checks-with-modular-arithmetic.html\">Joe Groff<\/a> (<a href=\"https:\/\/twitter.com\/jckarter\/status\/1035388126795980801\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/duriansoftware.com\/joe\/Optimizing-is-multiple-checks-with-modular-arithmetic.html\"><p>A related common operation, the one I want to focus on here, is to test whether a number is an exact multiple of another, using the remainder operator[&#8230;] <tt>clang<\/tt> and <tt>gcc<\/tt> leverage the same division-into-multiplication trick to optimize\nthis operation, first performing the division via multiplication, then\nmultiplying by the divisor and comparing the result to see if the result\nmatches[&#8230;]<\/p><p>[&#8230;]<\/p>\n<p>Can we do better? It\nturns out we can, using some tricks with modular arithmetic. Integer arithmetic\nin a 32- or 64-bit CPU register wraps if it overflows, effectively implementing\nthe integers modulo 2&#xB3;&#xB2; or 2&#x2076;&#x2074;. An interesting property of integers modulo 2&#x207F; is\nthat every odd number has a\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Modular_multiplicative_inverse\">modular inverse<\/a>,\nwhich is another number it can be multiplied with modulo 2&#x207F; to produce 1.<\/p><\/blockquote>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2010\/02\/17\/labor-of-division\/\">Dividing by Multiplying<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>John McCall: It is substantially more fluent than its standard implementation of value % divisor == 0. Divisibility testing is the operation people actually want; the % operator is just the way they have to achieve it. % is rarely used outside of this idiom, and many programmers have no other reason to know it. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-08-31T19:17:28Z","apple_news_api_id":"fbff21c4-c0b2-4e3d-9d72-a77f9f631efa","apple_news_api_modified_at":"2025-08-17T18:57:26Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A-_8hxMCyTj2dcqd_n2Me-g","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[255,46,259,138,71,901],"class_list":["post-22595","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-compiler","tag-languagedesign","tag-math","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=22595"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22595\/revisions"}],"predecessor-version":[{"id":48935,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22595\/revisions\/48935"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=22595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=22595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=22595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}