so I need an bitwise expression which will output 1
for the input 1 , and 8 for the input 4
Input: 1 > Output:1
Input:4 > Output:8
The expression should be 5-6 chars long.
arithmetics operations are allowed
Thank you :)
Homework?
Assuming unsigned char a;
a << (a >> 2)
If arithmetic operations are allowed it can be a bit shorter:
a << a/4
Related
Input :
cout << (4 % ((10^9)+1) );
Output :
0
Compiler : g++ 4.8.4
I don't know why the compiler printing the value is 0. What i have to do now for expected output?
What is the correct code to print the output?
^ is a bitwise exclusive OR operation, not exponentation. 10 ^ 9 is 3.
So 4 % ((10 ^ 9) + 1) = 4 % (3 + 1) = 4 % 4 = 0. The compiler is correct.
Did you mean std::pow(10L, 9L), taking care not to overflow an integral type? Then the answer would be 4 (although you would have to cast the return of std::pow to an appropriate integral type for the % operator).
((10^9)+1) = 4
4 % 4 = 0
The % operator returns the remainder of the division. Example, 4/3 = 0 and 3 as remainder so it will return 3, but 4/4 = 1 and no remainder, so it returns 0.
The compiler shows the correct answer.
(10^9) = 3
((3) + 1) = 4
4 % (4)= 0
The modulus (%) operator returns the remainder after a divison. See this page for a tutorial on the modulus operator.
Thank you for explaining this logic. I have understood this logic with your help and here is the solution for this logic that i got solved.
cout << (4 % ((10^9)+1) ); ==> cout <<fmod(5, (pow(10,9)+1));
My mistake is used ^ instead of pow() and % instead of fmod().
^ operator is bitwise xor in C++. (Just now realised afer your comments. Thanks for the info guys).
% operator is for integers. So, i have used fmod() function. Because my calculation need more than integer type.
I don't know why the compiler printing the value is 0. What i have to
do now for expected output?
(10^9) == 3
^ operator is bitwise xor in C++. That is, in binary
decimal binary
10 0110
9 0101
0110 ^ 0101 == 0011
and thus:
10^9 == 3
Consequently:
(10^9) + 1 == 4
and
4 % ((10^9) + 1) == 0
If we have a bitmask -> "00101101"
and a variable i = 4
If we do this (1 << i) | bitmask then what will happen to the bitmask?
Thank you.
Your question is:
If we do this (1 << i) | bitmask then what will happen to the bitmask?
Well, bitmask is involved in a statement the side effects of which do not affect bitmask itself.
So, nothing happens to bitmask, it was and it remains 00101101.
So you have:
unsigned int i = 4;
and you do:
1 << i;
this shifts 1 over 4 binary bits, you then do:
i | bitmask;
this logically return an unsigned int with the 4th bit set plus all the 1 bits in bitmask (which remains untouched) set.
i = 4, 1<<4 is shift left by 4 digits to one ====> reults 10000 ====> 16
I have the following code
int n = 50;
while(n) { //1
if(n & 1) cout << "1" << endl; //2
//right shift the number so n will become 0 eventually and the loop will terminate
n >>= 1; //3
}
When we use bitwise and 1 (& 1) with a number we get back the same number.
Now my question is how does c++ evaluates the following expression: n & 1.
Since:
n = 50
In binary form 50 is: 110010
If we bitwise 1 then we get: AND 1 = 110010
Now in c++ (2) the expression evaluates like this:
Instead of getting the whole sequence of bits (110010) bitwise anded with 1
it evaluates only the number of right bits we bitwise. In my example:
n=50, 110010, use n & 1 ==> 0 AND 1 instead of 110010 AND 1.
Is there a reason that c++ treats the bitwise and like this? My guess would be it has to do with the compiler ?
When we use bitwise and 1 (& 1) with a number we get back the same number.
No we don't. We get back the number consisting of the bits that are set in both the original number and in 1. Since only the lowest bit of 1 is set, the result is the lowest bit of the original number.
Now my question is how does c++ evaluates the following expression: n & 1.
If n is 50, then in binary:
n: 110010
1: 000001
n&1: 000000 // no bits set in both
If n is 51, then in binary:
n: 110011
1: 000001
n&1: 000001 // one bit set in both
From Wikipedia:
The bitwise AND operator is a single ampersand: &. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.
In your example 110010 & 1, 1 is considered as 000001, and then each bit is anded and you get the result. In fact, I use this method: 1&number to check for even and odd numbers. This is how:
if(1 & num)
printf("it is odd");
else
printf("it is even");
This is how it works: suppose you have an 8 bit number. Now, the 8 bit notation of 1 will be 00000001.
If I now perform and on each bit, for all the first seven bits I will get 0, because it will be 0 & anything will be 0. Now, the last bit of 1 is 1. So, if my number also has last bit as 1, then 1 & 1 = 1, and if my last bit is 0, then 1 & 0 = 0.
When will the last bit in my number be 1? And when 0? When converting to decimal form, the last bit is multiplied by 20. And, 20 = 1. If this 1 is multiplied with 1, we get an odd number, and if it is multiplied with 0, we get an even number.
I came from this question where I wanted to write 2 integers to a single byte that were garunteed to be between 0-16 (4 bits each).
Now if I close the file, and run a different program that reads....
for (int i = 0; i < 2; ++i)
{
char byteToRead;
file.seekg(i, std::ios::beg);
file.read(&byteToRead, sizeof(char));
bool correct = file.bad();
unsigned int num1 = (byteToRead >> 4);
unsigned int num2 = (byteToRead & 0x0F);
}
The issue is, sometimes this works but other times I'm having the first number come out negative and the second number is something like 10 or 9 all the time and they were most certainly not the numbers I wrote!
So here, for example, the first two numbers work, but the next number does not. For examplem, the output of the read above would be:
At byte 0, num1 = 5 and num2 = 6
At byte 1, num1 = 4294967289 and num2 = 12
At byte 1, num1 should be 9. It seems the 12 writes fine but the 9 << 4 isn't working. The byteToWrite on my end is byteToWrite -100 'œ''
I checked out this question which has a similar problem I think but I feel like my endian is right here.
The right-shift operator preserves the value of the left-most bit. If the left-most bit is 0 before the shift, it will still be 0 after the shift; if it is 1, it will still be 1 after the shift. This allow to preserve the value's sign.
In your case, you combine 9 (0b1001) with 12 (0b1100), so you write 0b10011100 (0x9C). The bit #7 is 1.
When byteToRead is right-shifted, you get 0b11111001 (0xF9), but it is implicitly converted to an int. The convertion from char to int also preserve the value's sign, so it produce 0xFFFFFFF9. Then the implicit int is implicitly converted to a unsigned int. So num1 contains 0xFFFFFFF9 which is 4294967289.
There is 2 solutions:
cast byteToRead into a unsigned char when doing the right-shift;
apply a mask to the shift's result to only keep the 4 bits you want.
The problem originates with byteToRead >> 4 . In C, any arithmetic operations are performed in at least int precision. So the first thing that happens is that byteToRead is promoted to int.
These promotions are value-preserving. Your system has plain char as signed, i.e. having range -128 through to 127. Your char might have been initially -112 (bit pattern 10010000), and then after promotion to int it retains its value of -112 (bit pattern 11111...1110010000).
The right-shift of a negative value is implementation-defined but a common implementation is to do an "arithmetic shift", i.e. perform division by two; so you end up with the result of byteToRead >> 4 being -7 (bit pattern 11111....111001).
Converting -7 to unsigned int results in UINT_MAX - 6 which is 4295967289, because unsigned arithmetic is defined as wrapping around mod UINT_MAX+1 .
To fix this you need to convert to unsigned before performing the arithmetic . You could cast (or alias) byteToRead to unsigned char, e.g.:
unsigned char byteToRead;
file.read( (char *)&byteToRead, 1 );
When I run this simple code,
int main(int argc, const char * argv[])
{
bool digit(true);
std::cout << digit << " " << ~digit << std::endl;
}
The output is
1 -2
I was expecting 1 and 0 (for true and false). Am I missing something here?
~ performs bitwise negation. The operand is promoted (in this case) to int, and all the bits are inverted. 1 has a binary representation of 00....001, so this gives the binary value 11....110, which is interpreted (on most modern computers) as -2.
Use ! for logical negation.
~ is the bitwise not (or bit inversion) operator. The logical not operator is '!'.
cout << !digit;
Essentially:
1 -> 00000001
~1 -> 11111110
You need a logical not operator is '!'.You may try this;
cout << !(digit);
EDIT:-
Although I know its late but trying to improve my answer, ~ is the bitwise not operator. So if you write something like ~1000 then it would result out to be 0001
4 bits integer
1 -> 0001
find the complement by adding 1
0001 + 0001 = 0010 = 2
the complement is -2