Created
November 12, 2024 02:12
-
-
Save jarodsim/4e8a0e3bc474748bcbe8be469078ae88 to your computer and use it in GitHub Desktop.
decimal_to_binary.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decimal_to_binary_32bit(number): | |
return format(number, '032b') | |
addresses = [3, 180, 43, 2, 191, 88, 190, 14, 181, 44, 186, 253] | |
results = [] | |
num_blocks = 16 | |
index_bits = 4 | |
def calculate_tag_index(address): | |
binary_address = decimal_to_binary_32bit(address) | |
index = binary_address[-index_bits:] | |
tag = binary_address[:-index_bits] | |
return binary_address, tag, index | |
cache = [{"tag": None} for _ in range(num_blocks)] | |
def check_cache(address, tag, index): | |
index_value = int(index, 2) | |
cache_block = cache[index_value] | |
if cache_block["tag"] == tag: | |
return "Acerto" | |
else: | |
cache[index_value]["tag"] = tag | |
return "Falha" | |
for address in addresses: | |
binary_address, tag, index = calculate_tag_index(address) | |
result = check_cache(address, tag, index) | |
results.append((address, binary_address, tag, index, result)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment