I have a weird issue that I cant seem to resolve so hope that converting this to some other form will help:
|coder response|
(coder isBitSet: 1)
ifFalse:[self flagSuccess]
ifTrue:[self flagFailure].
now the issue is coder is a value from 0 to F, when I get a 5 I want it to be treated as 0101, so that it is FALSE from isBitSet:. BUT isBitSet: treats it as 101, so it's always true... so basically isBitSet: isn't working for any binary number thats 4 bits long UNLESS the number is zero...
how can I get my five so that I can check the 4th bit in the number for a 1 or 0?
Try using the bitAt: method. This method extracts a bit from an integer and tells you whether it's a 1 or a 0. It will extract any bit you want and treat bits higher than the size of the integer as 0's.
5 bitAt: 1 ==> 1
5 bitAt: 2 ==> 0
5 bitAt: 3 ==> 1
5 bitAt: 4 ==> 0
5 bitAt: 5 ==> 0
Does that help?
You might not be interpreting the bit numbering correctly. The reason why 5 isBitSet: 1 evaluates to true is that 1 refers to the lowest bit. Regardless of whether 5 is represented as 101, 0101 or even 00101 etc., the lowest bit is always 1 and 5 isBitSet: 1 answers with true.
Related
For example, if n=9, then how many different values can be represented in 9 binary digits (bits)?
My thinking is that if I set each of those 9 bits to 1, I will make the highest number possible that those 9 digits are able to represent. Therefore, the highest value is 1 1111 1111 which equals 511 in decimal. I conclude that, therefore, 9 digits of binary can represent 511 different values.
Is my thought process correct? If not, could someone kindly explain what I'm missing? How can I generalize it to n bits?
29 = 512 values, because that's how many combinations of zeroes and ones you can have.
What those values represent however will depend on the system you are using. If it's an unsigned integer, you will have:
000000000 = 0 (min)
000000001 = 1
...
111111110 = 510
111111111 = 511 (max)
In two's complement, which is commonly used to represent integers in binary, you'll have:
000000000 = 0
000000001 = 1
...
011111110 = 254
011111111 = 255 (max)
100000000 = -256 (min) <- yay integer overflow
100000001 = -255
...
111111110 = -2
111111111 = -1
In general, with k bits you can represent 2k values. Their range will depend on the system you are using:
Unsigned: 0 to 2k-1
Signed: -2k-1 to 2k-1-1
What you're missing: Zero is a value
A better way to solve it is to start small.
Let's start with 1 bit. Which can either be 1 or 0. That's 2 values, or 10 in binary.
Now 2 bits, which can either be 00, 01, 10 or 11 That's 4 values, or 100 in binary... See the pattern?
Okay, since it already "leaked": You're missing zero, so the correct answer is 512 (511 is the greatest one, but it's 0 to 511, not 1 to 511).
By the way, an good followup exercise would be to generalize this:
How many different values can be represented in n binary digits (bits)?
Without wanting to give you the answer here is the logic.
You have 2 possible values in each digit. you have 9 of them.
like in base 10 where you have 10 different values by digit say you have 2 of them (which makes from 0 to 99) : 0 to 99 makes 100 numbers. if you do the calcul you have an exponential function
base^numberOfDigits:
10^2 = 100 ;
2^9 = 512
There's an easier way to think about this. Start with 1 bit. This can obviously represent 2 values (0 or 1). What happens when we add a bit? We can now represent twice as many values: the values we could represent before with a 0 appended and the values we could represent before with a 1 appended.
So the the number of values we can represent with n bits is just 2^n (2 to the power n)
The thing you are missing is which encoding scheme is being used. There are different ways to encode binary numbers. Look into signed number representations. For 9 bits, the ranges and the amount of numbers that can be represented will differ depending on the system used.
This is not connected to any particular programming language, I'm trying to look for the most efficient way to store a binary grid (It will look like this...)
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Into an int with a limit of 9,999,999
So each "1" Have a Boolean of true/false a.k.a "1" or "0" state so it can be displayed as also
1 0 0 1 1
1 1 0 1 1
1 1 1 1 1
All this is expected to be stored in an int so it can be transformed binary into digits.
So at the end of the day, I should be able to first set a binary grid then turn the binary grid into digits where I can store them into a variable, And then be able to extract them after from the variable back to the binary grid.
And also to answer any question related to why I just don't simply use an array or anything in relation, Well basically this is the only way the engine lets me store things to then extract them later.
Things I try: I already know I can store things within the digit system, What I lack is getting the most out of it, right now I can store 23 Bits which means that I got to get a binary grid that looks like this
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1
And remember this is having the limit of 9,999,999
I found out the system can have it from -9,999,999 to 9,999,999.
Another thing is that the limit of 9,999,999 is all-around any function so adding 9,999,999 + 9,999,999 is out of the question for that limitation.
So I'm going to add all the relevant details:
I can only use digits to store data in a variable.
The limit of the digits go only to 9,999,999
If I were to use floats it will have to be < 999,999.00 note that the limit on the float is 2 decimal points (0.00)
If I want, I can use multiple variables in terms that have more capacity
What I'm looking for is to try to get the most amount of bits out of the digits limit in the variable.
In this example, it is 5 - 2 which equals 3. How does the computer know that the outcome isn't negative though and why does it not think the answer is 35?
0101 = 5
+1110 = -2
-----------
10011 = 3
What does the 1 and the front of the answer represent?
The leading one is called overflow.
You are operating on a 4 bit byte if the operation you are using does not move overflow to another byte it will be lost.
0101 = 5
+1110 = -2
-----------
0011 = 3
Usually processors have a series of flags that can be set by operations in this case the overflow flag will be set to true. As the result exceeds what can be stored in a byte.
So in binary to find the largest number you can represent given N amount of bits, you would use:
2^N - 1
But why the -1. To try understand it i created a 3 Bit systems and tried some examples:
2^1 = (2) - 1
0 0 1 --> 1
2^2 = (4) - 1
0 1 0 --> 2
0 1 1 --> 3
2^3 = (8) - 1
1 0 0 --> 4
1 0 1 --> 5
1 1 0 --> 6
1 1 1 --> 7
So it all works out as planned, but why the -1. This probably sounds like a stupid question but as you can see above i have done a fair amount of research.
Because you can represent 0 which always takes up one spot in all the permutations.
The research shown should reveal the answer already, but you have forgotten about the zero.
Three bits are able to represent 2^3 different values. The smallest value is zero, so the largest must be 2^3-1.
Note that if you use a different system (such as signed binary), the smallest and largest value may change, but the count of values does not.
For example, if n=9, then how many different values can be represented in 9 binary digits (bits)?
My thinking is that if I set each of those 9 bits to 1, I will make the highest number possible that those 9 digits are able to represent. Therefore, the highest value is 1 1111 1111 which equals 511 in decimal. I conclude that, therefore, 9 digits of binary can represent 511 different values.
Is my thought process correct? If not, could someone kindly explain what I'm missing? How can I generalize it to n bits?
29 = 512 values, because that's how many combinations of zeroes and ones you can have.
What those values represent however will depend on the system you are using. If it's an unsigned integer, you will have:
000000000 = 0 (min)
000000001 = 1
...
111111110 = 510
111111111 = 511 (max)
In two's complement, which is commonly used to represent integers in binary, you'll have:
000000000 = 0
000000001 = 1
...
011111110 = 254
011111111 = 255 (max)
100000000 = -256 (min) <- yay integer overflow
100000001 = -255
...
111111110 = -2
111111111 = -1
In general, with k bits you can represent 2k values. Their range will depend on the system you are using:
Unsigned: 0 to 2k-1
Signed: -2k-1 to 2k-1-1
What you're missing: Zero is a value
A better way to solve it is to start small.
Let's start with 1 bit. Which can either be 1 or 0. That's 2 values, or 10 in binary.
Now 2 bits, which can either be 00, 01, 10 or 11 That's 4 values, or 100 in binary... See the pattern?
Okay, since it already "leaked": You're missing zero, so the correct answer is 512 (511 is the greatest one, but it's 0 to 511, not 1 to 511).
By the way, an good followup exercise would be to generalize this:
How many different values can be represented in n binary digits (bits)?
Without wanting to give you the answer here is the logic.
You have 2 possible values in each digit. you have 9 of them.
like in base 10 where you have 10 different values by digit say you have 2 of them (which makes from 0 to 99) : 0 to 99 makes 100 numbers. if you do the calcul you have an exponential function
base^numberOfDigits:
10^2 = 100 ;
2^9 = 512
There's an easier way to think about this. Start with 1 bit. This can obviously represent 2 values (0 or 1). What happens when we add a bit? We can now represent twice as many values: the values we could represent before with a 0 appended and the values we could represent before with a 1 appended.
So the the number of values we can represent with n bits is just 2^n (2 to the power n)
The thing you are missing is which encoding scheme is being used. There are different ways to encode binary numbers. Look into signed number representations. For 9 bits, the ranges and the amount of numbers that can be represented will differ depending on the system used.