Bits Required to Store Values - binary

How many bits are required to store three different values?
My guess is 8 times 3 = 24 bits.
I'm confused because I learned in class that a 8-bit byte can only hold a value between 0 and 255. Does this mean to store a value above 255 we need more than 8 bits?

A bit is either 0 or 1. So it can store 2 values.
Two bits can store 2*2 values, 4.
Three bits can store 2*2*2 values, 8.
And so on.
So to store 3 values, you need at least two bits.
You need more than 8 bits to store more than 256 values, yes, because 2^8 is 256.

TL;DR: two digits.
In any number base system, the number of values that can be held by a single digit is equal to the base number, so for the regular base-10 number system, that would be 10 (0 through 9). To hold higher numbers, you give yourself more digits: this would be the ones place, tens place, hundreds place, etc you learned in school.
Once you start giving yourself more digits, then it's just a matter of combinations: in a two-digit base-10 number, how many combinations are there? 00 through 99, so 100; with three digits, 000 through 999, so 1000.
The name bit is nothing more than the special name given to the digit in base-2 (otherwise known as binary). It can hold exactly two values, 0 and 1. To get more, you'd have to give yourself another digit, e.g., the ones place, the twos place. And even more digits: the fours place, the eights place, etc. Again, it's nothing but combinatory math. Two base-2 digits (or binary digits, or bits) can hold values 00 through 11, so 4; with three bits 00 through 111, so 8; with four bits 00 through 1111, so 16; with eight (a byte) 00000000 through 11111111, so 256.
So, in order to hold three values - e.g., 0 1 and 2 base-10 - you'd need two digits: 00 01 and 10 binary.

Related

how many bits correspond to N hexadecimal digits?

The answer is not so simple. I know that generally 1 hex digit is mapped on 4 bits, but this is not even true. What appen if the MSB (the most left) is a 0?
Online I noticed that this string is converted sometimes to 476 digits, sometimes to 478 digits and sometimes to 480 digits? How do you convert it? And why? It's 120 digit long. Thank you.
02012B1530A6E3958A98530031902003876940000000000CDF9844173BE512AFFFFFFE11DBBA1F00079387800E13012E11FC017FFFFFFFFE39C10F40
It is true, that one hex digit corresponds to 4 bits.
So you just covert
0 => 0000
...
f => 1111
When you do this with the given number it starts with 0000 0010. Like with decimal numbers, leading zeros can be removed or added as you like, so all answers with more or less leading zeros can be correct.
Hex and binary are just other number systems like decimal.
In case of integers, you typically want to remove all leading zeros, lime with decimal numbers.
but when it is a byte or bit array that represents data, you would keep them, because 0 actually represents the value 0.
Ihr calculator that gives you 480 as result probably interprets it as a byte or bit array and therefore keeps the leading zeros.
476 seems to remove leading hex zeros and then keeps eventual the up to 3 leading binary zeros. No idea how you would end up with 478 digits.
However the common outputs should have 480 or 474 digits, depending if all zeros are kept or none. Everything else does not really make sense, eventhoug it is not technically wrong.

largest integer that can be stored in a double such that all integers less than can be accurately stored as well

This is some more clarification to the question that was already answered some time ago here: biggest integer that can be stored in a double
The top answer mentions that "the largest integer such that it and all smaller integers can be stored in IEEE 64-bit doubles without losing precision. An IEEE 64-bit double has 52 bits of mantissa, so I think it's 2^53:
because:
253 + 1 cannot be stored, because the 1 at the start and the 1 at the end have too many zeros in between.
Anything less than 253 can be stored, with 52 bits explicitly stored in the mantissa, and then the exponent in effect giving you another one.
253 obviously can be stored, since it's a small power of 2.
Can someone clarify the first point? What does he mean by that? is he talking about for example if it were a 4 bit number 1000 + 0001, you can't store that in 4 bits? 253 is just the first bit 1 and the rest 0's right? how come you can't add a 1 to that without losing precision?
also, "The largest integer such that it and all smaller integers can be stored in IEEE". Is there some general rule such that if I wanted to find the largest n bit integer such that it and all smaller integers can be stored in IEEE, could I simply say that it is 2n? example if I were to find the largest 4 bit integers such that it and all integer below it can be represented, it would be 2^4?
is he talking about for example if it were a 4 bit number 1000 + 0001, you can't store that in 4 bits?
No, he is saying that you can't store that in 3 bits. Using the usual binary notation.
253 is just the first bit 1 and the rest 0's right?
Yes, and so are 1, 2, 4, …, 253, 254, 255, …, 2123, 2124, … and also 0.125.
This is floating-point we are talking about. 253 is just an implicit 1 with all explicit significand bits 0, yes, but it is not the only number with this property. The crucial property is that the ULP for representing 253 is 2. So 253 can be represented as all powers of two that are in range, and 253+1 cannot because the ULP is too large in that neighborhood.
also, "The largest integer such that it and all smaller integers can be stored in IEEE". Is there some general rule such that if I wanted to find the largest n bit integer such that it and all smaller integers can be stored in IEEE, could I simply say that it is 2n?
Yes, in binary IEEE 754 floating-point, all “largest integer such that it and all smaller integers can be stored” are powers of two, and specifically 2n where n is the significand's width (counting the implicit bit).

Is this just a coincidence that 0xaaaaaaaa represents binary with even positions set as 1

Is this just a coincidence that hexadecimal 0xaaaaaaaa represents binary with even positions set as 1.
Similarly something as elegant as 0x55555555 represents binary with odd positions set as 1 ?
Binary representation of 5 is 0101. So 0X55555555 has 16 ones, 16 zeros and the ones,zeros take alternate positions. Similarly 0X33333333 has 16 ones, 16 zeros and 2 consecutive ones, 2 consecutive zeros alternate.
Nothing special about those numbers per se, other than the fact that their corresponding bit patterns are useful.
I think the key realization here is that it's super easy to come up with a compact hex number to represent any longer bit pattern (even easier if it's repeating), right off the top of your head.
Why? Because it's trivial to convert from hex-to-binary or binary-to-hex - every four bits of the pattern can be neatly represented by one hex digit:
So let's say I wanted this 16-bit mask: 1110111011101110. This is 1110 repeated 4 times, so it's just some hex digit, 4 times. Since 1110 is 14 in decimal, that's gonna be "E", so our mask would be: 0xEEEE.

Largest memory space that can be addressed

I was asked the following question on an assignment, but I'm not sure if I did it correctly.
"What is the largest memory space (i.e. program) that can be addressed by processors with the following number of address bits?
(c) 24 bits"
I put 011111111111111111111111 (0 followed by 23 1s). Is this correct? If not, how do I find the answer to this question? You can use a different amount of bits for an example if you want to. Thanks for any help.
No 011111111111111111111111 is not the correct answer. I'm assuming that you were calculating the largest number that can be represented by a signed 24 bit integer.
Memory address would be always unsigned so the answer is the number items that can be represented in 24 bits, which is 2^24 or 1000000000000000000000000 which is 1 followed by 24 zeros (assuming that I counted correctly) - since the address range includes 0 and goes to 1111111111111111111111 (24 1's).
2^N bytes, where N is number of bits in the address space.
For example, the 8088 processor had a 20 bit address space and so it could address 2^20 bytes = 1 MB.
Address space is unsigned so N is full number of bits, not number of bits minus 1.
An address in programming is usually something that represents a location in memory.
You can always represent as many locations as there are unique numbers.
How many locations can you address with a range from 1 to 10? 10.
How many locations can you address with a range from 1 to 2^24? 2^24.
So you can represent 2^24 locations and you didn't answer correctly.
You're on the wrong track.
Memory addresses are unsigned, so the size of the address space is 2^24 bytes, or 16Mb.
If you had 2 bits you could go from 00 to 11, 00, 01, 10, 11, four addresses. Four is 1 with two zeros 0. Two address bits one with two zeros is the number of addresses or 2 to the power 2. 3 bits 0b1000 or 8 addresses 2 to the power 3, 4 bits 0b10000 or 16 addresses, 2 to the power 4 and so on to whatever number of bits you want.

Decimal/Hexadecimal/Binary Conversion

Right now I'm preparing for my AP Computer Science exam, and I need some help understanding how to convert between decimal, hexadecimal, and binary values by hand. The book that I'm using (Barron's) includes an example but does not explain it very well.
What are the formulas that one should use for conversion between these number types?
Are you happy that you understand number bases? If not, then you will need to read up on this or you'll just be blindly following some rules.
Plenty of books would spend a whole chapter or more on this...
Binary is base 2, Decimal is base 10, Hexadecimal is base 16.
So Binary uses digits 0 and 1, Decimal uses 0-9, Hexadecimal uses 0-9 and then we run out so we use A-F as well.
So the position of a decimal digit indicates units, tens, hundreds, thousands... these are the "powers of 10"
The position of a binary digit indicates units, 2s, 4s, 8s, 16s, 32s...the powers of 2
The position of hex digits indicates units, 16s, 256s...the powers of 16
For binary to decimal, add up each 1 multiplied by its 'power', so working from right to left:
1001 binary = 1*1 + 0*2 + 0*4 + 1*8 = 9 decimal
For binary to hex, you can either work it out the total number in decimal and then convert to hex, or you can convert each 4-bit sequence into a single hex digit:
1101 binary = 13 decimal = D hex
1111 0001 binary = F1 hex
For hex to binary, reverse the previous example - it's not too bad to do in your head because you just need to work out which of 8,4,2,1 you need to add up to get the desired value.
For decimal to binary, it's more of a long division problem - find the biggest power of 2 smaller than your input, set the corresponding binary bit to 1, and subtract that power of 2 from the original decimal number. Repeat until you have zero left.
E.g. for 87:
the highest power of two there is 1,2,4,8,16,32,64!
64 is 2^6 so we set the relevant bit to 1 in our result: 1000000
87 - 64 = 23
the next highest power of 2 smaller than 23 is 16, so set the bit: 1010000
repeat for 4,2,1
final result 1010111 binary
i.e. 64+16+4+2+1 = 87 in decimal
For hex to decimal, it's like binary to decimal, only you multiply by 1,16,256... instead of 1,2,4,8...
For decimal to hex, it's like decimal to binary, only you are looking for powers of 16, not 2. This is the hardest one to do manually.
This is a very fundamental question, whose detailed answer, on an entry level could very well be a couple of pages. Try to google it :-)