I've just started learning. I am trying to understand this thoroughly and deeply. I somewhat understand it as long as it's subtraction between a Smaller number from a Larger number or a Larger number from a Smaller number, but when the Minuend in the question is also Negative it confuses me. Which value do I get the complement of?
When asked to do a problem such as, 0101 - 1100, when do I treat the 1 as a negative bit instead of just an unsigned bit? When would I read it as 5-12 instead of 5-(-3)?
How do you solve 0101 - 1100 using One's Complement? Is it possible? Do I treat the question as asking me to subtract 12 from 5 instead of -3 from 5.
How do you subtract a number from a negative number? For example, -5-7? How do you do this using One's Complement only? Do you get the complement of 7 or 5 or both and add them? How would it change in Two's Complement?
Please someone can clarify this for me, I want to understand this and move on from this.
So after scouring other sources and getting some help, I have cleared all this up. I will answer it helps anyone that comes across this.
1. Identifying whether to read 0101 - 1100 as 5-12 or 5-(-3) can only be done if there is context given as to whether it's signed or unsigned.
2. With Ones complement, 0101 - 1100 = 5-(-3) = 5+3.
We get Ones complement of 1100 by negating/inverting = 0011 = 3. Then we do addition:
5+3 = 0101 + 0011 = 1000
The answer is not 8 and is a negative result.
This means it has overflowed, because we added two values of the same sign and got a result with the opposite sign. The overflow occurs because 4 bits Ones complement has a range of -7 to +7. The 1 in 1000 occupies the positional value -(2^(4-1))-1 which is -7. So it has gone from +7 to -7 with the extra 1 value (8-7 = +1), meaning the -7 is essentially +7 + 1 and that additional 1 has overflowed into -7. This because the 8th value in the range starting from 0 is -7 in 4 bits Ones complement representation. Consider this:
Values from -7 to +7 can form a wheel. -7,-6,-5,-4,-3,-2,-1,-0,0,1,2,3,4,5,6,7
If you keep adding 1 to each value above it will take you to the next value and
keep going in a circle in binary form.
Example:
+7 = 0111
0111 + 1 = 1000 = -7
1000 + 1 = 1001 = -6
1001 + 1 = 1010 = -5 and on on and on.
So the calculation is correct, just that the 4 bit ones complement representation cannot represent a +8 and overflows to a -7.
**3.**In this case it underflows, where the negative number of the -5-7 is too low to be represented with Ones Complement and Twos Complement in 4 bits representation.
-5 in Ones' Complement = 1010, -7 in Ones' Complement = 1000
-5-7 = -a-b = -(a+b)
So mathematically you can look at it as 1010+1000 or -(0101+0111)
1010 0101
+1000 +0111
=Carry->1 0010 = -(1100)= -(-3)= 3
+ 1
= 0011= 3
So we end up with the same answer regardless, I'd recommend the first method though so that it doesn't lead to confusion.
The result is +3 after adding two negative values, and the value itself is 3, which shows that it has underflowed because -12 cannot be represented in 4 bit Ones Complement as the values are only from -7 to +7 as mentioned above.
So referring to the list of the range of values above. If you count backwards 7 units you end up at +3. If the range of values contained -12 value then counting back from -5 will have you end up at -12.
With Twos complement, the difference is that you add 1 to the ones complement values to get Two's Complement. Then add them the same way.
-5 in Ones' Complement = 1010, -7 in Ones' Complement = 1000
Two's Complement = 1011, Two's Complement = 1001
1011
+1001
= Overflow bit->1 0100 = +4
In Two's Complement calculation the Overflow bit is ignored if you get it when adding two values of different signs, or when adding the same sign you get an answer with the same sign. In the above case however, it is 0100 which is +4, after adding two negative values. So it indicates overflow.
The reason the value is +4 instead of +3 like in Ones' Complement is because there is an extra negative value in Two's Complement range compared to One's Complement as Two's Complement doesn't have a negative 0.
The range of values for Two's Complement 4 bit representation is:
-2^(4-1) to +(2^(4-1))-1 which is equal to -8 to +7
-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7
If you count backwards 7 units from -5, you end up at +4. Since -12 cannot still
be represented between -8 to +7, +4 is where you end up with the overflow.
I hope this clears up things and helps someone. Please let me know if there are any mistakes and I'll correct them.
Related
I would like to convert $14.83984375$ to Binary using IEEE 754 with 32 bits.
The true solution is: $01000001011011010111000000000000$.
But I do not get it.
My solution is:
1. the sign is positive: 0
2. 14 in binary equals $1110$ $0.83984375$ equals $110101100...0$
3. So the exponent is 3 and we get: $3+127 = 10000010$ in bits
So if we put that together we get
$0 10000010 110110101100...0$
The failure is, that I have $0.83984375$ equals $110101100...0$ and the solution says $0.83984375$ equals $110101110...0$.
Where does the $1$ come from?
Thank you in advance.
You didn't convert 0.83984375 correctly; it is 0.11010111.
I want to write a regular expression for Binary Numbers Divisible by 5.
I have already done the regular expressions for Binary Numbers Divisible by 2 and for 3 but I couldn't find one for 5.
Any suggestions?
(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$ to test it with regexp. See it working here.
You can build a DFA and convert it to regular expression. The DFA was already built in another answer. You can read it, it is very well explained.
The general idea is to remove nodes, adding edges.
Becomes:
Using this transformation and the DFA from the answer I linked, here are the steps to get the regular expression:
(EDIT: Note that the labels "Q3" and "Q4" have been mistakenly swapped in the diagram. These states represent the remainder after modulus 5.)
2^0 = 1 = 1 mod 5
2^1 = 2 = 2 mod 5
2^2 = 4 = -1 mod 5
2^3 = 8 = -2 mod 5
2^4 = 16 = 1 mod 5
2^5 = 32 = 2 mod 5
... -1 mod 5
... -2 mod 5
So we have a 1, 2, -1, -2 pattern. There are two subpatterns where only the sign of the number alternates: Let n is the digit number and the number of the least significant digit is 0; odd pattern is
(-1)^(n)
and even pattern is
2x((-1)^(n))
So, how to use this?
Let the original number be 100011, divide the numbers digits into two parts, even and odd. Sum each parts digits separately. Multiply sum of the odd digits by 2. Now, if the result is divisible by sum of the even digits, then the original number is divisible by 5, else it is not divisible. Example:
100011
1_0_1_ 1+0+1 = 2
_0_0_1 0+0+1 = 1; 1x2 = 2
2 mod(2) equals 0? Yes. Therefore, original number is divisible.
How to apply it within a regex? Using callout functions within a regex it can be applied. Callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching.
However, ndn's answer is more appropriate and easier, therefore I recommend to use his answer.
However, "^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))1)$" matches empty string.
I have to solve a question saying
The value of ab if ab & 0x3f equals 0x27.
For solving,I assumed let ab be 0xMN and then N&f means N&1111 but here book says that N&1111 should yield 0111 i.e. 7 =>N should be 7.
Similarly how M&0011should yield 2the book says without telling how.
This maybe quite a simple to you people but i tried didn't succeed so here.
Think of it this way - a bit can only have 2 possible values i.e. 0 or 1. If you write down the binary representations, you will end up with something like the below:
abcd efgh
& 0011 1111
-----------
= 0010 0111
Going by the definition of bitwise AND, the values of a to h that would give the below result are 0,0,1,0,0,1,1,1 respectively. Converting back to hexadecimal, the value is 0x27.
As #jlahd points out in the comments below, three more values can satisfy this equation : 0x67, 0xa7 and 0xe7. This is because x&0 = 0 so that a and b in the above could take any possible values without affecting the outcome.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Ok so Im a bit lost on this. So looking at a this 0 0 0 0 and looking at each 0 individually with each one has a probability of 0.01 of turning from a 0 to a 1
Ok so there are 4 numbers each number can be a 0 or a 1
Probability is measured by determining how likely an event is to happen divided by the number of possible outcomes
The possible outcomes are:
0000
0001 0010 0100 1000
1100 1001 1010 0101 0011 0110
1110 0111 1011 1101
1111
So there is a total of 16 possible outcomes or 4^2 i.e. 4 number by the possible outcomes they can be 0 or 1
So the probability of the string of number to be to turn from 0000 to 1111 would be
0.01/16
The probability of the number to be from 0000 to 0100 would also be
0.01/16?
And the probability of all the number to stay the same would i.e 0000 to 0000
would still be 0.01/16
It kinda makes sense but I don't know would all of them be the same probability?
Or am I doing this wrong
so each number has a 0.01 chance to change so for the string to go from 0000 to 1111 would be 0.01 *4 / 16 or 0.0025
And the chance to change from 0000 to 0100
would be 0.01 * 1 /16
And to change from 0000 to 0000 would be
0.01*0 /16
Thanks for any help with this
There are two things here. One, the number of possible outcomes (16) and the probability of each outcome.
Since the probability of a single bit being flipped to 1 is skewed towards it being 0, then the distribution of outcome probabilities is not even. If there was a 50% chance of a 1 or a 0 in each spot, then the probability of each of the 16 outcomes would be 1/16. That is not the case here.
The approach I'd take is to group the numbers into buckets. Of the 16 outcomes, 1 has zero 1's, 4 have one 1, 6 have two 1's, 4 have three 1's, and 1 has four 1's.
The probability of four 0's is 99%^4. The probablity of one 1 is 1% x 99%^3. The probability of 2 is 1%^2 x 99%^2. etc. Calculate each of the probabilities , then divide by the size of the bucket. Add them up, and they should equal 100% (sanity check).
I checked it in a spreadsheet, and the results seem good:
Outcome Probability
0000 0.96059601
0001 0.00970299
0010 0.00970299
0100 0.00970299
1000 0.00970299
0011 0.00009801
0101 0.00009801
1001 0.00009801
0110 0.00009801
1010 0.00009801
1100 0.00009801
0111 0.00000099
1011 0.00000099
1101 0.00000099
1110 0.00000099
1111 0.00000001
You state that the probability of a zero changing to one is .01 (I'm inferring then .99 of no change)
The Overall probability is the product of single probabilities then:
Lets do a simple case:
0000 to 1111
(.01) * (.01) * (.01) * (.01) = (.01)^4 = .00000001
0000 to 0100
(.99) * (.01) * (.99) * (.99) = (.99)^3 * (.01) = .00970299
So they don't have the same probability
I am not able to understand what is meant by the following text, which I referred from a book:
Consider the four two-bit numbers
00, 01, 10, 11. If you add up
the one’s bit, you get an even number.
Likewise, if you add up two’s bit, you
get an even number. No matter how many
bits in the number, if you add up a
column, you get an even number.
Specifically, what is meant by "add up one's bit" for 00?
They just mean that if you write the four numbers in a column:
00
01
10
11
...and you look at how many bits in the first column (the "one's bits") are 1, you get an even number. Similarly for the second column (the "two's bits").
Their claim is that no matter how many bits the number has, if you write down all numbers with that many bits, the number of 1's in each column will be even.
Their claim is false for one-bit numbers. In general, for n bits, the number of 1s in each column will (obviously) be 2^(n-1), which is even except when n=1.
What book is this? What point are they trying to make?
The bits in a binary number are generally "named" column-wise according to their respective power of two:
00000000
│││││││└── 1's bit
││││││└─── 2's bit
│││││└──── 4's bit
││││└───── 8's bit
│││└────── 16's bit
││└─────── 32's bit
│└──────── 64's bit
└───────── 128's bit
The "one's" bit is the bit that represents a "one", i.e. the rightmost bit. The "two's" bit is the bit used to hold a 2, i.e. the bit second to the right. The next bit to the left of the "two's" bit is the "four", etc.
001 = 1
010 = 2
100 = 4
Thinking back to the way numbers in decimal numbers. For example: 184. Starting at the right-most number, we have 4, it's equivilent to saying "there are 4 ones in this numbers." It's in the ones place, as we progress left we have the 8 in the tens place (meaning to represent that there are 8 tens) and 1 in the hundreds place (only 1 hundred). For a binary number like 10 (binary for 2), the 0 in the right-most place is in the ones place, that "column" (position from the right hand side) is the place that denotes how many 1's are in that number. Along the same lines, the 1 is in the twos place, denoting how many twos are in this number.
i think what it is trying to say is that if you take all the numbers with a certain number of bits - in this example, 2 then add the values of each column, the result will be even.
so, for the 4 2 bit numbers, add each column separately:
0 0
0 1
1 0
+1 +1
- -
2 2
each column adds to 2 - an even number
similarly, for all the 3 bit numbers:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
+1 +1 +1
- - -
4 4 4
each column adds to 4 - even