#include
using namespace std;
int main()
{
int x=1,y=1;
for(;y;cout<
{
y=x++ <=5;
}
return 0;
}
The output of the program is :21 31 41 51 61 70
How does this work?
Answers
Answer:
Explanation:
ok
Answer:
Explanation:
bitwise Complement: The bitwise complement operator, the tilde, ~, flips every bit. The tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit:
This turns out to be a great way of finding the largest possible value for an unsigned number (see Two's Complement):
unsigned int max = ~0;
bitwise AND: The bitwise AND operator is a single ampersand: &:
01001000 &
10111000 =
--------
00001000
bitwise OR: The bitwise OR operator is a |:
01001000 |
10111000 =
--------
11111000
bitwise Exclusive OR (XOR): The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a carrot, ^, performs the exclusive-or operation on each pair of bits. Exclusive-or is commonly abbreviated XOR.
01110010 ^
10101010
--------
11011000
Suppose, we have some bit, either 1 or 0, that we'll call Z. When we take Z XOR 0, then we always get Z back: if Z is 1, we get 1, and if Z is 0, we get 0. On the other hand, when we take Z XOR 1, we flip Z. If Z is 0, we get 1; if Z is 1, we get 0:
myBits ^ 0 : No change
myBits ^ 1 : Flip
It's a kind of selective twiddle(~)..
So, if we do XOR against 111...1111, all the bits of myBits flipped. It's equivalent of doing twiddle(~)..
Suppose we have number 8 written in binary 00001000. If we wanted to shift it to the left 2 places, we'd end up with 00100000; everything is moved to the left two places, and zeros are added as padding. This is the number 32:
00001000(8) << 2
--------
00100000(32)
Actually, left shifting is the equivalent of multiplying by a power of two:
x << n
--------
x * (1 << n)
More specifically:
8 << 3
--------
8 * (1 << 3)
--------
8 * (2**3)
--------
64
Set a bit (where n is the bit number, and 0 is the least significant bit):
unsigned char a |= (1 << n);
Example:
a 1 0 0 0 0 0 0 0
a |= (1 << 1) = 1 0 0 0 0 0 1 0
a |= (1 << 3) = 1 0 0 0 1 0 0 0
a |= (1 << 5) = 1 0 1 0 0 0 0 0
Clear a bit:
unsigned char b &= ~(1 << n);
Example 1:
b 1 1 1 1 1 1 1 1
b &= ~(1 << 1) = 1 1 1 1 1 1 0 1
b &= ~(1 << 3) = 1 1 1 1 0 1 1 1
b &= ~(1 << 5) = 1 1 0 1 1 1 1 1