Can someone please explain to me how overflow works in binary coding?

Watch
john2054
Badges: 6
Rep:
?
#1
Report Thread starter 4 years ago
#1
I understand how to do negative numbers (convert then plus one) to the code, but i'm still not sure about over/under flow (in binary code). Also hexidecimal, base 15 right? But I still haven't fully got my head around that also! Help!!?!
0
reply
Matrix123
Badges: 18
Rep:
?
#2
Report 4 years ago
#2
(Original post by john2054)
x
Hexadecimal is base 16

Posted from TSR Mobile
0
reply
Aklaol
Badges: 12
Rep:
?
#3
Report 4 years ago
#3
(Original post by john2054)
I understand how to do negative numbers (convert then plus one) to the code, but i'm still not sure about over/under flow (in binary code). Also hexidecimal, base 15 right? But I still haven't fully got my head around that also! Help!!?!
In binary?
Very simple.... let's say you have this binary addition:Image

The last bit on the left cannot be stored as the value is too big. Instead of the expected 8 bits total, this addition has produced 9 bits. So in this case, the CPU drops the overflow digit, as it cannot be stored.
0
reply
john2054
Badges: 6
Rep:
?
#4
Report Thread starter 4 years ago
#4
(Original post by Aklaol)
In binary?
Very simple.... let's say you have this binary addition:Image

The last bit on the left cannot be stored as the value is too big. Instead of the expected 8 bits total, this addition has produced 9 bits. So in this case, the CPU drops the overflow digit, as it cannot be stored.
Okay so if an 8 bit system were to max out exactly, it just resets? Is that right? And then say if it were two added, instead of one, what then?

11111111+00000010=?
0
reply
BobBobson
Badges: 3
Rep:
?
#5
Report 4 years ago
#5
pragmatically, yes, it pretty much resets, but that's not technically whats going on. What happens is that numbers such as 100000000 in binary cant be stored in 1 byte, so the 1 gets dropped off, and you're left with 00000000. If you add 2, it's similar 11111111 + 00000010 = 10000001, but thats too big, so the left 1 gets dropped off and youre left with 00000001.

Posted from TSR Mobile
0
reply
Aklaol
Badges: 12
Rep:
?
#6
Report 4 years ago
#6
(Original post by john2054)
Okay so if an 8 bit system were to max out exactly, it just resets? Is that right? And then say if it were two added, instead of one, what then?

11111111+00000010=?
It doesn't matter how many extra bits are added. If it's an overflow error, the CPU will simply drop all the extra bits. If the CPU physically can't store a piece of data, then it can't do anything else other than to drop the extra bits. If a 8 bit system was to max out on memory, virtual memory would be created. If all else fails and there isn't any main memory, virtual memory or hard disk space left, then the CPU wouldn't store anymore data.
0
reply
BobBobson
Badges: 3
Rep:
?
#7
Report 4 years ago
#7
Also Hex is just base sixteen, it uses 16 digits from 0-F. So 10 would be "A" in hex , 15 would be "F" in hex and 16 would be "10". Hex is useful for doing stuff like bit masks and bit flags and is pretty much just concise binary. One hex digit can store the same as 4 binary digits. For example if you want a byte that is 255, rather than writing " 11111111" in binary, you just write "FF" in hexadecimal. A practical example of this is RGBA colour codes. These are 4 byte numbers that represent all of the colours. In binary, that would be 32 binary digits, but in hex, it's only 8 and thats why many colour codes are written in Hex.

Why use Hex and not just Decimal?
I believe that computers convert from Hex to binary much quicker than it can with decimal since 16 is exactly 2^4

Posted from TSR Mobile
0
reply
john2054
Badges: 6
Rep:
?
#8
Report Thread starter 4 years ago
#8
(Original post by BobBobson)
pragmatically, yes, it pretty much resets, but that's not technically whats going on. What happens is that numbers such as 100000000 in binary cant be stored in 1 byte, so the 1 gets dropped off, and you're left with 00000000. If you add 2, it's similar 11111111 + 00000010 = 10000001, but thats too big, so the left 1 gets dropped off and youre left with 00000001.

Posted from TSR Mobile
That answered my question thanks Bob.
0
reply
john2054
Badges: 6
Rep:
?
#9
Report Thread starter 4 years ago
#9
(Original post by BobBobson)
Also Hex is just base sixteen, it uses 16 digits from 0-F. So 10 would be "A" in hex , 15 would be "F" in hex and 16 would be "10". Hex is useful for doing stuff like bit masks and bit flags and is pretty much just concise binary. One hex digit can store the same as 4 binary digits. For example if you want a byte that is 255, rather than writing " 11111111" in binary, you just write "FF" in hexadecimal. A practical example of this is RGBA colour codes. These are 4 byte numbers that represent all of the colours. In binary, that would be 32 binary digits, but in hex, it's only 8 and thats why many colour codes are written in Hex.

Why use Hex and not just Decimal?
I believe that computers convert from Hex to binary much quicker than it can with decimal since 16 is exactly 2^4

Posted from TSR Mobile
I thought you said that 16 was 10 in hex i mean, or was that 01? oh dear i am getting confused.
0
reply
Joinedup
Badges: 20
Rep:
?
#10
Report 4 years ago
#10
(Original post by john2054)
Okay so if an 8 bit system were to max out exactly, it just resets? Is that right? And then say if it were two added, instead of one, what then?

11111111+00000010=?
The CPU (usually) has carry and overflow flags in it's status register - you can look at these after doing your arithmetic to see what happened... i.e. you don't just get a 'wrong answer' you get some diagnostics you can choose to look at or not.

http://teaching.idallen.com/dat2343/...0_overflow.txt
0
reply
john2054
Badges: 6
Rep:
?
#11
Report Thread starter 4 years ago
#11
(Original post by BobBobson)
Also Hex is just base sixteen, it uses 16 digits from 0-F. So 10 would be "A" in hex , 15 would be "F" in hex and 16 would be "10". Hex is useful for doing stuff like bit masks and bit flags and is pretty much just concise binary. One hex digit can store the same as 4 binary digits. For example if you want a byte that is 255, rather than writing " 11111111" in binary, you just write "FF" in hexadecimal. A practical example of this is RGBA colour codes. These are 4 byte numbers that represent all of the colours. In binary, that would be 32 binary digits, but in hex, it's only 8 and thats why many colour codes are written in Hex.

Why use Hex and not just Decimal?
I believe that computers convert from Hex to binary much quicker than it can with decimal since 16 is exactly 2^4

Posted from TSR Mobile
16=00 in hex right?
0
reply
john2054
Badges: 6
Rep:
?
#12
Report Thread starter 4 years ago
#12
(Original post by Joinedup)
The CPU (usually) has carry and overflow flags in it's status register - you can look at these after doing your arithmetic to see what happened... i.e. you don't just get a 'wrong answer' you get some diagnostics you can choose to look at or not.

http://teaching.idallen.com/dat2343/...0_overflow.txt
Sorry joined up, but i've only been doing this java course for two days, and that overfow flag stuff you just shared, goes like waay over my head!
0
reply
BobBobson
Badges: 3
Rep:
?
#13
Report 4 years ago
#13
Lemme just count up for you in hex:

1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10 ,11,12,13,14,15,16,17,18,19,1A,1 B,1C,1D,1E,1F

Thats 1-31 in hexadecimal

Posted from TSR Mobile
0
reply
john2054
Badges: 6
Rep:
?
#14
Report Thread starter 4 years ago
#14
(Original post by BobBobson)
Lemme just count up for you in hex:

1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10 ,11,12,13,14,15,16,17,18,19,1A,1 B,1C,1D,1E,1F

Thats 1-31 in hexadecimal

Posted from TSR Mobile
20,21,22,23,24,25,26,27,28,29,2a ,2b,2c,2d,2e,2f,30,31,32,33,34,3 5,36,37,38,39,3a,3b,3c,3d,3e,3f and is that 32-63? how do you remember all of this?
0
reply
BobBobson
Badges: 3
Rep:
?
#15
Report 4 years ago
#15
(Original post by john2054)
20,21,22,23,24,25,26,27,28,29,2a ,2b,2c,2d,2e,2f,30,31,32,33,34,3 5,36,37,38,39,3a,3b,3c,3d,3e,3f and is that 32-63? how do you remember all of this?
yep thats right. Hexadecimal is rarely used for counting and maths, its mainly used for storing binary concisely, so you dont need to memorise every numbers equivalent in hex. You just need to know how to work it out.

Posted from TSR Mobile
2
reply
john2054
Badges: 6
Rep:
?
#16
Report Thread starter 4 years ago
#16
(Original post by BobBobson)
yep thats right. Hexadecimal is rarely used for counting and maths, its mainly used for storing binary concisely, so you dont need to memorise every numbers equivalent in hex. You just need to know how to work it out.

Posted from TSR Mobile
Is there a program you can use to convert hex to decimal?
0
reply
jordanshelley97
Badges: 9
Rep:
?
#17
Report 4 years ago
#17
(Original post by john2054)
Is there a program you can use to convert hex to decimal?
There are many online. http://www.binaryhexconverter.com/he...imal-converter

If you want to do it yourself then just go up in powers of 16 from the right, starting with to the power of zero.
For example if you had 6A4 then you would do this
4*16^0 = 4
A=10 so 10*16^1 = 160
6*16^2 = 1536

Add them all together you get 1700
1
reply
john2054
Badges: 6
Rep:
?
#18
Report Thread starter 4 years ago
#18
(Original post by jordanshelley97)
There are many online. http://www.binaryhexconverter.com/he...imal-converter

If you want to do it yourself then just go up in powers of 16 from the right, starting with to the power of zero.
For example if you had 6A4 then you would do this
4*16^0 = 4
A=10 so 10*16^1 = 160
6*16^2 = 1536

Add them all together you get 1700
here i will set myself a task 1af3b8=
8*16^0=8
11*16^1=176
3*16^2=768
15*16^3=61440
10*16^4=655360
1*16^5=1048576

corrected sum=1766328
this checks out
Also can a hexidecimal bit be 0?
0
reply
jordanshelley97
Badges: 9
Rep:
?
#19
Report 4 years ago
#19
(Original post by john2054)
here i will set myself a task 1af3b8=
8*16^0=8
11*16^1=176
3*16^2=768
16*16^3=65536
10*16^4=655360
1*16^5=1048576
sum=1770424 is this right?

Also can a hexidecimal bit be 0?
One mistake is that F is actually 15 so it would actually be 15*16^3=61440
That's quite a common mistake though. We know hex is base-16 and we know F is the last letter so we think automatically that F must be 16, I've done it myself
But apart from that the rest is correct. And yes any of the bits can be 0
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (686)
33.89%
Yes, I like the idea of applying to uni after I received my grades (PQA) (860)
42.49%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (387)
19.12%
I think there is a better option than the ones suggested (let us know in the thread!) (91)
4.5%

Watched Threads

View All
Latest
My Feed