Address on Same Page
Problem statement
You are given two unsigned 32-bit memory addresses, addr1 and addr2.
Memory is split into aligned 4 KB pages. Each page contains 4096 consecutive byte addresses: the first page covers addresses 0 through 4095, the next covers 4096 through 8191, and so on.
Return 1 if both addresses belong to the same page. Otherwise, return 0.
Two addresses share a page when their highest 20 bits match. Their lowest 12 bits give the offset within the page and may differ.
Function
samePage(addr1: long, addr2: long) → intExamples
Example 1
addr1 = 0addr2 = 4095return = 1Addresses 0 and 4095 both lie in the first page, which covers addresses 0 through 4095.
Example 2
addr1 = 4095addr2 = 4096return = 0Address 4095 is in the page from 0 through 4095. Address 4096 starts the next page, so the addresses are on different pages.
Example 3
addr1 = 65536addr2 = 69631return = 1Both addresses lie in the page from 65536 through 69631. Their offsets differ, but their page number is 16.
Constraints
0 <= addr1 <= 2^32 - 10 <= addr2 <= 2^32 - 1