Bit-wise operations on addresses - bit-manipulation

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.

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.

converting CFG to regular expression

Here's a CFG that generates strings of 0s, 1s, or 0s and 1s arranged like this (001, 011) where one of the characters must have a bigger count than the other like in 00011111 or 00000111 for example.
S → 0S1 | 0A | 0 | 1B | 1
A → 0A | 0
B → 1B | 1
I tried converting it to regular expression using this guide but I got stuck here since I have trouble converting 0S1 given that anything similar to it can't be found in that guide.
S → 0S1 | 0+ | 0 | 1+ | 1
A → 0A | 0 = 0+
B → 1B | 1 = 1+
One of my previous attempts is 0+0+1|0+1+1|1+|0+ but it doesn't accept strings I mentioned above like 00011111 and 00000111.
Plug and Play
^(?!01$)(?!0011$)(?!000111$)(?!00001111$)(?=[01]{1,8}$)0*1*$
You cannot perfectly translate this to a regular expression, but you can get close, by ensuring that the input does not have equal number of 0 and 1. This matches up to 8 digits.
How it works
^ first you start from the beginning of a line
(?!01$) ensure that the characters are not 01
(?!0011$) ensure that the characters are not 0011
the same for 000111 and 00001111
then ensure that there are from 1 to 8 zeroes and ones (this is needed, to ensure that the input is not made of more digits like 000000111111, because their symmetry is not verified)
then match these zeroes and ones till the end of the line
for longer inputs you need to add more text, for up to 10 digits it is this: ^(?!01$)(?!0011$)(?!000111$)(?!00001111$)(?!0000011111$)(?=[01]{1,10}$)0*1*$ (you jump by 2 by adding one more symmetry validation)
it is not possible by other means with regular expressions alone, see the explanation.
Explanation
The A and B are easy, as you saw 0+ and 1+. The concatenations in S after the first also are easy: 00+, 0, 11+, 1, that all mixed into one lead to (0+|1+). The problem is with the first concatenation 0S1.
So the problem can be shorten to S = 0S1. This grammar is recursive. But neither left linear nor right linear. To recognize an input for this grammar you will need to "remember" how many 0 you found, to be able to match the same amount of 1, but the finite-state machines that are created from the regular grammars (often and from regular expressions) do not have a computation history. They are only states and transitions, and the machinery "jumps" from one state to the other and does not remember the "path" traveled over the transitions.
For this reason you need more powerful machinery (like the push-down automaton) that can be constructed from a context-free grammar (as yours).

How this language has even number of symbols ((0+1)(0+1))*

I am studying course for Automata, may be as a beginner I'm missing something , I am really confused with this regular expression as the teacher saying
it represents a language with all binary strings having even number of
symbols
Alphabet Σ={0,1}
((0+1)(0+1))*
(0+1) saying union of 0 and 1
but I am confused here let suppose
first (0+1) gives 01 and second (0+1) gives only 0, then finally we concatenate both having (010)* and let it end up with only one occurrence i.e. 010
then how it has even number of symbols? need little understanding of this...
The given Question: ((0+1)(0+1))*
Let's start from the innermost bracket (0+1) this matches with either 0 or 1 not 01
'+' means "or" operator, this is not a conjunction operator.
and this is repeated twice hence ((0+1)(0+1)) matches with 01 or 10 or 00 or 11
and '*' means it matches 0 or more times
Hence, ((0+1)(0+1))* matches with [NULL] or 01 or 10 or 00 or 11 or 0011 or 1100 or 1010 or 0110 ....so on
Union means one or the other, not both. So, 0+1 would either be 0 or 1, but not both at the same time. Hence (0+1)(0+1) would result in either 00, 01, 10 or 11. All of them have even number of symbols. The result would follow.

What is meant by adding a column in bits?

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

Regexes for integer constants and for binary numbers

I have tried 2 questions, could you tell me whether I am right or not?
Regular expression of nonnegative integer constants in C, where numbers beginning with 0 are octal constants and other numbers are decimal constants.
I tried 0([1-7][0-7]*)?|[1-9][0-9]*, is it right? And what string could I match? Do you think 034567 will match and 000083 match?
What is a regular expression for binary numbers x such that hx + ix = jx?
I tried (0|1){32}|1|(10)).. do you think a string like 10 will match and 11 won’t match?
Please tell me whether I am right or not.
You can always use http://www.spaweditor.com/scripts/regex/ for a quick test on whether a particular regex works as you intend it to. This along with google can help you nail the regex you want.
0([1-7][0-7])?|[1-9][0-9] is wrong because there's no repetition - it will only match 1 or 2-character strings. What you need is something like 0[0-7]*|[1-9][0-9]*, though that doesn't take hexadecimal into account (as per spec).
This one is not clear. Could you rephrase that or give some more examples?
Your regex for integer constants will not match base-10 numbers longer than two digits and octal numbers longer than three digits (2 if you don't count the leading zero). Since this is a homework, I leave it up to you to figure out what's wrong with it.
Hint: Google for "regular expression repetition quantifiers".
Question 1:
Octal numbers:
A string that start with a [0] , then can be followed by any digit 1, 2, .. 7 [1-7](assuming no leading zeroes) but can also contain zeroes after the first actual digit, so [0-7]* (* is for repetition, zero or more times).
So we get the following RegEx for this part: 0 [1-7][0-7]*
Decimal numbers:
Decimal numbers must not have a leading zero, hence start with all digits from 1 to 9 [1-9], but zeroes are allowed in all other positions as well hence we need to concatenate [0-9]*
So we get the following RegEx for this part: [1-9][0-9]*
Since we have two options (octal and decimal numbers) and either one is possible we can use the Alternation property '|' :
L = 0[1-7][0-7]* | [1-9][0-9]*
Question 2:
Quickly looking at Fermat's Last Theorem:
In number theory, Fermat's Last Theorem (sometimes called Fermat's conjecture, especially in older texts) states that no three positive integers a, b, and c can satisfy the equation an + bn = cn for any integer value of n greater than two.
(http://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem)
Hence the following sets where n<=2 satisfy the equation: {0,1,2}base10 = {0,1,10}base2
If any of those elements satisfy the equation, we use the Alternation | (or)
So the regular expression can be: L = 0 | 1 | 10 but can also be L = 00 | 01 | 10 or even be L = 0 | 1 | 10 | 00 | 01
Or can be generalized into:
{0} we can have infinite number of zeroes: 0*
{1} we can have infinite number of zeroes followed by a 1: 0*1
{10} we can have infinite number of zeroes followed by 10: 0*10
So L = 0* | 0*1 | 0*10
max answered the first question.
the second appears to be the unsolvable diophantine equation of fermat's last theorem. if h,i,j are non-zero integers, x can only be 1 or 2, so you're looking for
^0*10?$
does that help?
There are several tool available to test regular expressions, such as The Regulator.
If you search for "regular expression test" you will find numerous links to online testers.