{"id":40,"date":"2026-10-07T09:20:00","date_gmt":"2026-10-07T09:20:00","guid":{"rendered":"https:\/\/tothepowerof2.com\/blog\/?p=40"},"modified":"2026-10-07T02:56:08","modified_gmt":"2026-10-07T02:56:08","slug":"powers-of-two-memory-worked-examples","status":"publish","type":"post","link":"https:\/\/tothepowerof2.com\/blog\/powers-of-two-memory-worked-examples\/","title":{"rendered":"Powers of 2 in Computer Memory: Worked Examples"},"content":{"rendered":"<p><strong>TL;DR \/ Summary:<\/strong> Memory sizing problems reduce to identifying bit widths, byte counts, and repeated doublings. These worked examples walk through RAM modules, array footprints, and address calculations using 2^n. Double-check arithmetic with <a href=\"https:\/\/tothepowerof2.com\/\">our free power of 2 calculator<\/a>.<\/p>\n<h2>Example 1: unsigned byte range<\/h2>\n<p><strong>Question:<\/strong> How many distinct values can an 8-bit unsigned integer store?<\/p>\n<p><strong>Solution:<\/strong> 2^8 = 256 values, typically 0 through 255 inclusive. Off-by-one errors treat 255 as count\u2014remember inclusive range size formula: max &#8211; min + 1 = 256.<\/p>\n<h2>Example 2: array memory footprint<\/h2>\n<p><strong>Question:<\/strong> A float32 array holds 4096 elements. How many bytes?<\/p>\n<p><strong>Solution:<\/strong> Each float32 = 4 bytes = 2^2 bytes. Total = 4096 \u00d7 4 = 16384 bytes. Recognize 4096 = 2^12, so total = 2^12 \u00d7 2^2 = 2^14 = 16384 bytes = 16 KiB.<\/p>\n<h3>Example 3: pointer addressing<\/h3>\n<p><strong>Question:<\/strong> Thirty-two-bit pointers address how many bytes directly?<\/p>\n<p><strong>Solution:<\/strong> 2^32 bytes = 4 GiB. If each struct is 64 bytes = 2^6, maximum struct count \u2248 2^32 \/ 2^6 = 2^26 \u2248 67 million structs before virtual memory limits intervene.<\/p>\n<h2>Example 4: alignment padding<\/h2>\n<p><strong>Question:<\/strong> Struct size 100 bytes packed in 64-byte cache lines. How much waste for 10,000 structs naive layout?<\/p>\n<p><strong>Solution:<\/strong> Objects span ceil(100\/64) = 2 lines each \u2192 128 bytes allocated per object for alignment worst case. Overhead 28 bytes \u00d7 10000. Power-of-two line size 64 = 2^6 simplifies bitmask alignment: address &#038; 63 must be zero for line start.<\/p>\n<h2>Example 5: bitmap allocation<\/h2>\n<p><strong>Question:<\/strong> Track 65536 slots with one bit each. Bitmap size?<\/p>\n<p><strong>Solution:<\/strong> 65536 bits \/ 8 = 8192 bytes = 2^13 bytes. Slots = 2^16, bytes = 2^16 \/ 2^3 = 2^13.<\/p>\n<h2>Example 6: doubling heap growth<\/h2>\n<p><strong>Question:<\/strong> Dynamic array starts 16 elements, doubles when full. After how many doublings capacity reaches at least 10000?<\/p>\n<p><strong>Solution:<\/strong> Capacities: 16=2^4, then 32, 64, \u2026 find smallest k where 2^(4+k) \u2265 10000. 2^13 = 8192 insufficient; 2^14 = 16384 sufficient. k = 10 doublings from start? Track: n=0\u219216, after 10 doublings 16\u00d72^10 = 16384. Ten resize events.<\/p>\n<h2>Example 7: VRAM texture budget<\/h2>\n<p><strong>Question:<\/strong> One 2048\u00d72048 RGBA8 texture\u2014memory?<\/p>\n<p><strong>Solution:<\/strong> 2048 = 2^11. Pixels = 2^11 \u00d7 2^11 = 2^22. Bytes per pixel = 4 = 2^2. Total = 2^24 = 16 MiB per mip level base. Full mip chain \u2248 4\/3 \u00d7 base\u2014still power-of-two dominated.<\/p>\n<h2>Verification habit<\/h2>\n<p>After manual exponent algebra, plug intermediate 2^n values into trusted tools. <a href=\"https:\/\/tothepowerof2.com\/\">Calculate 2 to the power of N online<\/a> when chaining divides risks exponent subtraction mistakes.<\/p>\n<h2>Study workflow<\/h2>\n<p>Cover solution steps, attempt independently, reveal, compare exponent path versus decimal multiplication path. Both should match\u2014if not, find whether bit\/byte conflation caused drift.<\/p>\n<h2>Example 8: stack versus heap allocation<\/h2>\n<p><strong>Question:<\/strong> Thread stack size default 8 MiB on many Linux systems\u2014how many 2^10-byte pages?<\/p>\n<p><strong>Solution:<\/strong> 8 MiB = 8 \u00d7 2^20 bytes. Pages 4096 = 2^12 bytes. Count = 8 \u00d7 2^20 \/ 2^12 = 8 \u00d7 2^8 = 8 \u00d7 256 = 2048 pages per thread. Spawning 100 threads consumes roughly 800 MiB stack virtual address space before guard pages\u2014capacity planning uses these 2^n steps.<\/p>\n<h2>Example 9: database page size<\/h2>\n<p><strong>Question:<\/strong> SQLite default page 4096 bytes; database file 100 MB decimal approximate\u2014how many pages order-of-magnitude?<\/p>\n<p><strong>Solution:<\/strong> 100 \u00d7 10^6 \/ 4096 \u2248 24414 pages. Exact power-of-two path: 4096 = 2^12. Decimal bytes not power-of-two\u2014shows hybrid real-world problems mixing 10^n file labels with 2^n page sizes.<\/p>\n<h2>Peer review exercise<\/h2>\n<p>Swap worked examples with classmates; each finds one arithmetic step to challenge. Defending exponent subtraction in public solidifies learning faster than solitary rereading\u2014especially on divide problems like bitmap sizing.<\/p>\n<h2>Example 10: video frame buffer<\/h2>\n<p><strong>Question:<\/strong> 1920 by 1080 RGBA frame\u2014bytes per frame?<\/p>\n<p><strong>Solution:<\/strong> Pixels equal 1920 times 1080\u2014not power-of-two dimensions, because real content often is not. Bytes equal 1920 times 1080 times 4, which is 8,294,400 or about 7.91 MiB. Double-buffering for vsync doubles to roughly 15.8 MiB. Lesson: multiply actual dimensions; powers of two appear in bytes-per-pixel (4 equals 2^2) and MiB conversion (divide by 2^20), not always width and height.<\/p>\n<p>Compare to 1024 by 1024 RGBA equals 2^20 times 4, which is 4 MiB exactly\u2014idealized textbook case versus broadcast resolution homework.<\/p>\n<h2>Homework submission tips<\/h2>\n<p>Show exponent subtraction when dividing sizes\u2014 graders award partial credit for correct method with arithmetic slip. Box final answer with units: bytes, KiB, or bits explicitly.<\/p>\n<p>Include sanity check line: result must be divisible by 4 when counting float32 elements\u2014 catches forgotten times four step early.<\/p>\n<h2>Whiteboard interview tip<\/h2>\n<p>When interviewer asks buffer size, narrate exponent steps aloud while writing\u2014communication score often equals arithmetic score. Saying two to the twelfth bytes sounds professional versus mumbling four thousand ninety-six.<\/p>\n<h2>Finish with this habit: powers of 2 in Computer Memory<\/h2>\n<p>Memory worked examples combine 2^n bit counts, byte scaling, and real allocation patterns from arrays to GPU textures. Express sizes as exponents to multiply and divide by adding\/subtracting powers. Practice until 2^12, 2^16, and 2^20 feel as familiar as decimal thousands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step-by-step memory sizing for arrays, pointers, bitmaps, and GPU textures using 2^n.<\/p>\n","protected":false},"author":1,"featured_media":56,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-math-practice-examples"],"_links":{"self":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":3,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/media\/56"}],"wp:attachment":[{"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothepowerof2.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}