what is the meaning of the following expression in c++ - c++

What is the meaning of the following expression in c++?
(variable1 | (variable2 << 8))
What is the meaning of it? And what does it represent?

It concatenates the two variables.
Suppose you have two chars, a and b. a|b<<8 shifts the b 8 bits to the left, | sets every bit that is in a or b.
So in this example the result would be "ab".
'a' is 97, 'b' is 98, so bitwise the following happens:
a: 01100001
b: 01100010
b<<8: 0110001000000000
a|b<<8: 0110001001100001

| is Bitwise OR
<< is Bitwise left shift operator
(variable1 | (variable2 << 8))
Left Shifts the variable2(8 bit) by 8 and then ORs the result with variable1(8 bit), resulting output will combine two variables variable1 and variable2 to be represented as one variable(16 bit).

You might think of it as "concatenating" two variables in a bitwise fashion.
If:
x = 00000000 00001000 (16-bit binary)
y = 00000000 00100010 (16-bit binary)
Then:
(y << 8) = 00100010 00000000
x | (y << 8) = 00100010 00001000
What it actually means in the context of the code in which you found it is anybody's guess.
In actual fact, "concatenating" is not accurate if x has any bits set in the most significant byte:
If:
x = 01000000 00001000 (16-bit binary)
y = 00000000 00100010 (16-bit binary)
Then:
(y << 8) = 00100010 00000000
x | (y << 8) = 01100010 00001000

If variable1 and variable1 are 8-bit values, then it combines them into a single 16-bit value.

It would make sense if both variables where bytes. In that case it would combine them into one larger variable, so that first come 8 bits of variable2 and then 8 bits of variable1.

In your code, the 8 least significant (rightmost) bits of variable1 are appended to the bits of variable2 from the right, with the bits of variable2 being shifted left by 8.
If denote the bits of variable1 as
xxxxxxxxxxxxxxxxxxxxxxxxwxxxxxxx
and the bits of variable2 as
yyyyyyyyzyyyyyyyyyyyyyyyyyyyyyyy
then expression
(variable1 | (variable2 << 8))
would result in
zyyyyyyyyyyyyyyyyyyyyyyywxxxxxxx

I don't know what you mean by "meaning" - but this is one variable being bitwise OR'ed with another variable which has been left-shifted by 8 bits (which you can think of as being multiplied by 256).

If both variable1 and variable are less than 256, the statement is the same as variable1 + (variable2*256).
More generally though, | is binary or and and << is left shift.
So if we start with:
variable1 = 321;
variable2 = 123;
The binary values would be:
variable1 => 0000 0001 0100 0001
variable2 => 0000 0000 0111 1011
Left shifting variable2 by 8 results in:
0111 1011 0000 0000
So variable1 | (variable2 << 8) equals
0111 1011 0100 0001
Which is 32065. This is less than 31519 which is the result of (321 + (123 * 256)) because variable1 and variable2 << 8 have some bits in common.

Related

The meaning of 1llu in C++

Consider the i-th column of a binary matrix is denoted with matrix[i]. Let D be the number of columns of the matrix.
My question: What is the result of the following code. In fact, I can't understand the role of the 1llu expression.
matrix[i]^((1llu << D)-1)
This has to be looked at from the binary representation.
1llu means 1 represented as a unsigned long long.
...0000 0000 0000 0001
<< D shift that 1 left D amount of times (bits)
If D==5 then :
...0000 0000 0010 0000
- 1 subtract 1 from the shifted result ( which gives 1's on the positions 0 ~ D-1)
...0000 0000 0001 1111
The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
https://learn.microsoft.com/en-us/cpp/cpp/bitwise-exclusive-or-operator-hat?view=vs-2019
It is easy to explain with an example below:
Example 1: 1 << 34
Example 2: 1llu << 34
If the integer size if 32 bits, then Example 1 would produce 0x0000 0000 as 1 will fall off. Whereas, Example 2 would produce 0x0000 0004 0000 0000
So, it should be seen with the context of the what is the type / size of matrix element.

Convert an 8 bit data to 3 bit data

PROGRAMMING LANGUAGE: C
I've a 8 bit data with only 3 bit used, for example:
0110 0001
Where 0 indicate unused bit that are always set to 0 and 1 indicate bits that change.
I want to convert this 0110 0001 8 bit to 3 bit that indicate this 3 used bits.
For example
0110 0001 --> 111
0010 0001 --> 011
0000 0000 --> 000
0100 0001 --> 101
How I can do that with minimal operations?
You can achieve this with a couple of bitwise operations:
((a >> 4) & 6) | (a & 1)
Assuming you start from xYYx xxxY, where x is a bit you don't care about and Y a bit to keep:
left shift by 4 of a will result in xYYx, then masking with 6 (binary 110) will make sure only the second and third bit are retained, resulting in YY0 and preventing flipped x bits from messing up.
a & 1 selects the LSB, resulting in Y.
the two parts, YY0 and Y are combined using a | bitwise or, resulting in YYY.
Now you have the 3 bits you asked. But keep in mind that you can't address single bits, so it will still be byte-aligned as 00000YYY
You can get the k'th bit of n: (where n is 011000001)
(n & ( 1 << k )) >> k
(More details about that at StackOverflow)
so you use that to get bit 1,6 and 7 and just add those:
r=bit1+bit6*16+bit7*32

How to interpret this bit shift

I have the following bit shift:
1011 & (~0 << 2)
How do I work out the answer for this? In particular I am confused about what ~0 << 2 means - I know that the << operator is a bit shift, and that ~ represents 'not'.
What I have read is that ~0 is a sequence of 1s - but how is that true, and how many 1s are there??
Usually, an int is a 32-bit/4-byte value. So ~0 = 1111 1111 1111 1111 1111 1111 1111 1111
In your case it really doesn't matter.
You want to solve 1011 & (~0 << 2)
Let's go through your example in steps.
First thing that happens is the parenthesis:
(~0 << 2)
This is the bits 1111 shifted left by two bits. When a lift shift occurs the new added bits are 0s. Therefore (~0 << 2) equals:
(1111 << 2) = 1100
Finally you just need to do a bitwise and between 1011 and 1100 which ends up as
1000 = 8

Bit manipulation (clear n bits)

Going through the book "Cracking the coding interview" by Gayle Laakmann McDowell, in bit manipulation chapter, it posts a question:
Find the value of (assuming numbers are represented by 4 bits):
1011 & (~0 << 2)
Now, ~0 = 1 and shifting it two times towards the left yields 100 ( = 0100 to complete the 4 bits). Anding 1011 with 0100 equals 0000.
However, the answer i have is 1000.
~0 is not 1 but 1111 (or 0xf). The ~ operator is a bitwise NOT operator, and not a logical one (which would be !).
So, when shifted by 2 places to the left, the last four bits are 1100. And 1100 & 1011 is exaclty 1000.
~0 does not equal 1. The 0 will default to being an integer, and the NOT operation will reverse ALL the bits, not just the first.
~ is the Bitwise Complement Operator.
The value of ~0 should be 1111 in 4 bits .
1011 & (~0 << 2)
= 1011 & ( 1111 << 2)
= 1011 & 1100
= 1000
1011 & (~0 << 2)
~0 is not 1 but rather 11112 or 0xF16.
Shifting 1111 to the left twice gives 1100 (the two leftmost bits have been dropped and filled in with 0s from the right).
Adding 1011 & 1100 gives 1 in each bit position for which the corresponding bit position is 1, otherwise 0. This follows that the result is 1000.

C++: what does (a<<b) mean?

I have a C++ header file that contains the following definitions:
#define CACHE_NUM_WAYS (1<<1)
#define CACHE_DATA_SIZE (1<<8)
It is used as an integer in the rest of the code.
What does it mean? And what is its value?
1 << 1 means:
00000000 00000001 changes to 00000000 00000010
1 << 8 means:
00000000 00000001 changes to 00000001 00000000
It's a bit shift operation. For every 1 on the right, you can think of yourself as multiplying the value on the left by 2. So, 2 << 1 = 4 and 2 << 2 = 8. This is much more efficient than doing 1 * 2.
Also, you can do 4 >> 1 = 2 (and 5 >> 1 = 2 since you round down) as the inverse operation.
a<<b for integers means "shift left". The bitwise representation of a is shifted left b bits. This is the same as multiplying by (2 to the power of b).
So in your example, (1<<1) is 1*(2^1) is 2, (1<<8) is 1*(2^8) is 256.
It is worth pointing out that in general, as with other operators in c++, << may be overridden to perform other functions. By default, input/output streams override this operator to let you write concise code to send a bunch of parameters to the stream. So you may see code like this:
cout << something << somethingelse
and << does not mean left shift in this context.
<< is bitwise shift left (there is also >> bitwise shift right)
if you have 32 bit integer
1 = 00000000 00000000 00000000 00000001 = 1
1 << 1 = 00000000 00000000 00000000 00000010 = 2
1 << 8 = 00000000 00000000 00000001 00000000 = 256
Those are bitwise shift operators.
http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx
<< is shifting left so it is actually multiplying by 2 for << 1 and by 2^8 for << 8.
The operator << is a bitwise left-shift operator.
So when you write 1<<17, the binary representation of 1 is shifted left by 17 bits as:
//before (assume 1 is represented by 32-bit)
1 << 17
0000 0000 0000 0000 0000 0000 0000 0001 << 17 (before - binary representation)
//after
0000 0000 0000 0010 0000 0000 0000 0000 (after - binary representation)
a<<b means (a * pow(2,b))-> a * 2^b //left-sift operator <<
a>>b means (a / pow(2,b))-> a / 2^b //right-sift operator >>
In CPP, "<<" stands for the left-shift operator and ">>" stands for the right-shift operator.
x<<y is equivalent to multiplying x with 2^y and x>>y is equivalent to dividing x by 2^y.
In theoretical terms, the left shift operator shifts the bits of the first operand to the left and the operand on the right in this case decides the number of bits to shift to the left and similarly, the right shift operator shifts the bits of the first operand to the right and the operand on the right decides the number of bits to shift to the right.
1<<8
here is how i understood it.
in the most layman way, understand it as 1 (00000001) in binary number system is now promoted to the 8 places(bits) ahead from where it was earlier,
and for every place as we know it moves the value(2 in binary) gets exponentially multiplied
so it becomes 1*(2^8)=256.
so 1<<6 will be 1*(2^6)= 64 and so on