What is meant by adding a column in bits? - c++

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

Related

How to subtract negative numbers from negative numbers using Complement notation?

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.

Adding 1 to the end (smallest digit) of a float, whatever length it is

I want to add 1 to the very end of a float, like this:
0.0735 + 0.0001 = 0.0736
But I'll get different lengths of floats, and for each I want to add 1 to the very end, like this:
0.000398 would have to be added to 0.000399 and 0.000000281 would have to be 0.000000282.
Also, if it's something like this: 0.0000280, I need to add it to the 0, not the 8. Which makes me believe that I have basically one problem: I need to find the first number after 0 and then count 2 more whatever it is, and add 1 to it (basically keeping 3 numbers, even if there's a 0 after)
Is there a way to do that?
edit: I tried this already len(str(n)) but the problem is the zeros, as I don't have control over the length of the number, a number like this: 0.01 should be added to 0.0101 but if I have 0.0111 I want 0.0112. What I mean is, the program usually hides the zeroes, that's why I said I'd probably need to find the first non zero digit on it.
10^floor(log10(n)) will get you a 1 digit at the same position as the first digit (e.g. 0.0001 for 0.000399), so add 0.01 times that:
n = 0.0735
n += 0.01 * 10 ** math.floor(math.log10(n))

Regular Expression for Binary Numbers Divisible by 5

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.

how to map a specialized string into specified integer

I am doing some financial trading work. I have a set of stock symbols but they have very clear pattern:
it's composed of two characters AB, AC AD and current month which is a four digit number: 1503, 1504, 1505. Some examples are:
AB1504
AB1505
AC1504
AC1505
AD1504
AD1505
....
Since these strings are so well designed patterned, I want to map (hash) each of the string into a unique integer so that I can use the integer as the array index for fast accessing, since I have a lot of retrievals inside my system and std::unordered_map or any other hash map are not fast enough. I have tests showing that general hash map are hundred-nanoseconds latency level while array indexing is always under 100 nanos.
my ideal case would be, for example, AB1504 maps to integer 1, AB1505
maps to 2...., then I can create an array inside to access the information related to these symbols much faster.
I'm trying to figure out some hash algorithms or other methods that can achieve my goal but couldn't find out.
Do you guys have any suggestions on this problem?
You can regard the string as a variable-base number representation, and convert that to an integer. For example:
AC1504:
A (range: A-Z)
C (range: A-Z)
15 (range: 0-99)
04 (range: 1-12)
Extract the parts; then a hash function could be
int part1, part2, part3, part4;
...
part1 -= 'A';
part2 -= 'A';
part4 -= 1;
return (((part1 * 26 + part2) * 100 + part3) * 12 + part4;
The following values should be representable by a 32-bit integer:
XYnnnn => (26 * X + Y) * 10000 + nnnn
Here X and Y take values in the range [0, 26), and n takes values in the range [0, 10).
You have a total of 6,760,000 representable values, so if you only want to associate a small amount of data with it (e.g. a count or a pointer), you can just make a flat array, where each symbol occupies one array entry.
If you parse the string as a mixed base number, first 2 base-26 digits and then 4 base-10 digits you will quickly get a unique index for each string. The only issue is that if you might get a sparsely populated array.
You can always reorder the digits when calculating the index to minimize the issue mentioned above.
As the numbers are actually months I would calculate the number of months from the first entry and multiply that with the 2 digit base-26 number from the prefix.
Hope you can make some sense from this, typing on my tablet at the moment. :D
I assume the format is 'AAyymm', where A is an uppercase character yy a two digit year and mm the two digit month.
Hence you can map it to 10 (AA) + Y (yy) + 4 (mm) bits. where Y = 32 - 10 - 4 = 18 bits for a 32 bit representation (or 262144 years).
Having that, you can represent the format as an integer by shifting the characters to there place and shifting the year and month pairs to there places after converting these to an integer.
Note: There will always be gaps in the binary representation, Here the 5+5 bit representation for the characters (6 + 6 values) and in the 4 bit month representation (4 values)
To avoid the gaps change the representation to ABmmmm, were the pair AB is represented by a the number 26*A+B and mmmm is the month relative to some zero month in some year (which covers 2^32/1024/12 = 349525 years - having 32 bits).
However, you might consider a split of stock symbols and time. Combining two values in one field is usually troublesome (It might be a good storage format, but no good 'program data format').

Answering Queries on Binary array

Given a binary array of length <=10^5 and almost equal number of queries. Each query is given by two integers (l,r) for each query we have to computer the total number of consecutive 0's and 1's in the range [l,r].
If n is the length of the array then 1 <= l < r <= n.
For example:
if the binary array (1-indexed) is "011000" and say there are 5 queries:
1 3
5 6
1 5
3 6
3 4
Then the required answer is
1
1
2
2
0
I am aware that this can be solved by a linear time (worst case) algorithm for each query but due to the large number of queries it's not feasible.
Just wondering which is the most efficient way to achieve this?
You can do it with O(n) space complexity and O(log(n)) search time for each query. Calculate the counts for windows of size 1, 2, 4, .... For a given query you can find O(log(n)) windows (at most 2 windows of a particular size), summing which you can find your answer.
As Dukeling said in the comments, you can preprocess in O(n) to compute an array B where B[x] contains the total number of consecutive digits seen in [1..r].
This allows a query in O(1) to find the number of consecutive digits in the range [l,r] by using the array to count the total number in the range [1,r] and subtracting the number in the range [1,l].
Python code:
def preprocess(A):
last=A[0]
B=[0,0]
num_consecutive=0
for a in A[1:]:
if a==last:
num_consecutive+=1
B.append(num_consecutive)
last=a
return B
def query(B,l,r):
return B[r]-B[l]
A=[0,1,1,0,0,0]
B=preprocess(A)
print query(B,1,3)
print query(B,5,6)
print query(B,1,5)
print query(B,3,6)
print query(B,3,4)