How to handle INT overflow on limited RAM devices (any language)? -


i have task in school have answer question:

"a couple of students make calendar program days "absolute daynumber" defined amount of days january 1st year 0 current day. on many smaller computers maximum value int 32767. can happen if disregard this? how can solve issue?"

the question kind of confuses me. work in python , have read python automatically handles int overflows converting int long , other programming languages similar things (like example wrapping around negative number, aka 32768 becomes -32767). if programming languages didn't automatically handle overflows imagine error if number goes on 32767... right?

if wanted print "absolute daynumber" (assuming on 32767 , can use int's) wouldn't able because can't store value in int. impossible. if wanted print today's date take pc bios or use unix time instead.

for me seems answer question depends on want program do. teacher says no. says none of answers correct. don't understand kind of answer wants , supposed easy question since have done programming 2 months now. missing obvious? overthinking this?

this not language-agnostic, since, have observed, there languages handle integer overflows.

and has nothing ram.

it has contemporary cpu architecture , way integer numbers work. in short, cpu can work quantities of bits called "word". typically, registers in part of cpu deal integers can hold 1 word. in example, word size 16.

that is, maximun number (in binary) can store is:

1111 1111 1111 1111 

so, happens when add 1 this?

 1111 1111 1111 1111 +0000 0000 0000 0001 

well 1+1 10 (in binary), 0 in last position, have carry 1 over. causes result 0, plus 1 carried over. 1 stored in special place of cpu, , can utilized branch instructions, long not overwritten subsequent arithmetic operations. (usually, called carry bit cannot used in programming languages.)

so see here result of 65535+1 results in 0, seems wrong. need used idea integer arithmetic done modulo 2^n, n word size in bits. correct result 65536, , 65536 mod 2¹⁶ 0.

i hope teacher has told basics, otherwise question seem unfair.

so, can happen if disregard this? can wrong results.

how can solve it? use 2 integers represent numbers wouldn't fit in 1 integer. there many possibilities how this. 1 possibility easy understand not ideal regard memory efficiency be: represent number of days in 2 integers. in first integer, count thousands. in second, represent last 3 digits (in decimal notation). example, represent 432105, set first integer 432, second 105.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -