What does this command mean in a .c/.cpp file?
uFlags &= ~CHN_PANNING;
as per my research uFlags are Bit-Wise flags.
~ is bit negation. It turns 011001 into 100110, etc. If you & with the bit negation of a number, you essentially remove the 1-bits of the right hand side from the bits on the left hand side (1s turn to 0s after the bit negation and no bit can stay 1 if it's &-ed with 0. 0s turn to 1s and &-ing with 1 doesn't change the original bit).
bitset0 &=~ bitset1 (or bitset0 &~ bitset) is therefore a kind of a C/C++ idiom for (bit)set subtraction. It subtracts bitset1 from bitset0 (i.e. bitset0 \ bitset1 in math notation), possibly (in the = version) assigning the result to bitset0.
The bitwise-AND assignment operator &= gives to the expression
uFlags &= ~CHN_PANNING;
the same meaning as
uFlags = uFlags & ~CHN_PANNING;
Now, the & bit-wise operator is used to reset (clear) all those bits of flags to 0, witch have the corresponding bit of the mask reset (cleared) to 0. Other bits of flags remain unchanged.
For example:
11001011 (flags)
&
10101110 (mask)
-------------------
10001010 (result)
Unary operator ~ is the negation - it switches bits with value 0 to 1 and vice versa. It means that your mask ~CHN_PANNING will reset (clear) to 0 that bit (or those bits), which were set (to 1) in the original CHN_PANNING.
Related
I am learning a backtrack problem with memoization using bit-mask.
When checking if the i'th bit is set in a bit-mask, all the solutions I have come across are doing (mask >> i) & 1. I was wondering, why is the & 1 necessary? Isn't (mask >> i) a 1 when the i'th bit is set, and a 0 when the bit is not set? That already translate into true and false.
(mask >> i) cannot eliminate the higher bits.
For example, when mask = 5 (101 in binary) and i = 1, the value of (mask >> i) is 2. This evaluated as true, but the 2nd lowest bit is 0, so you fail to check the bit correctly.
Therefore, & 1 is necessary to eliminate the higher bits and check one specified bit correctly.
For example, if you want to check bit 0 for the mask 0b10 then the expression mask >> 0 yields the same value 0b10, that is not equal to 0. However, its bit 0 is equal to 0. So you need to write ( mask >> 0 ) & 1, or in general ( mask >> i ) & 1.
That is, higher bits that precede the i-th bit can be 1. Thus the expression mask >> i does not change their values. So the value of the expression can be unequal to 0 though the i-th bit itself is equal to 0.
The expression (mask >> i) keeps all the bits from the i-th. That means, if either the i-th, (i+1)-th, etc. is set, then it'll evaluate to true in an if-expression.
I don't understand why these are different
this is how I wrote it :
number &= 0 << time;
and this is how it should be written :
number &= ~(1 << time);
Bitwise operations are performed on all bits.
The result of 0 << time is timeth bit set to zero, and all other bits are zero. So all bits are zero. Bitwise and with zero will always result in zero.
Contrary, the result of 1 << time is timeth bit set to one, and all other bits are zero. After bitwise negation, the result is timeth bit set to zero, and all other bits are one. Bitwise and will zero out only timeth bit.
I have this code:
if((bob.prop & 0x100) == 0x100) {
// some code
}
I found that this part:
bob.prop & 0x100
means
is bob.prop set to 0x100?
So, I think it gives me true or false. But the result of this code is comparing with 0x100 also:
if((bob.prop & 0x100) == 0x100) {
What is it? What does it mean?
0x100 in binary is 0000000100000000, which has its 9th bit set.
Doing bob.prop & 0x100 filters out that 9th bit from bob.prop. For example:
bob.prop & 0x100
1010110101110010 & 0000000100000000 gives 0000000100000000
0110110001101011 & 0000000100000000 gives 0000000000000000
It does this because it is the bitwise AND of the two operands. That means the result will only have bits set where bits are set in both of the operands.
You are then checking whether the result is equal to 0000000100000000, which is the same as asking "Was the 9th bit set?"
It is not necessary to perform this final comparison, because any integer value greater than 0 will convert to true, while 0 will convert to false. You could just write:
if(bob.prop & 0x100)
the bob.prop & 0x100 is doing a bitwise and of bob.prop with 0x100. The if statement is ensuring that equals 0x100.
So the first test is evaluating an expression, and putting it in the if checks if it matches 0x100.
Note that you are not testing that bob.prop is set to 0x100, rather than are checking whether one bit is set. So the expression would be true for input of 0x100, 0x101, 0x102, 0xfff, ...
I don't understand what this code is doing at all, could someone please explain it?
long input; //just here to show the type, assume it has a value stored
unsigned int output( input >> 4 & 0x0F );
Thanks
bitshifts the input 4 bits to the right, then masks by the lower 4 bits.
Take this example 16 bit number: (the dots are just for visual separation)
1001.1111.1101.1001 >> 4 = 0000.1001.1111.1101
0000.1001.1111.1101 & 0x0F = 1101 (or 0000.0000.0000.1101 to be more explicit)
& is the bitwise AND operator. "& 0x0F" is sometimes done to pad the first 4 bits with 0s, or ignore the first(leftmost) 4 bits in a value.
0x0f = 00001111. So a bitwise & operation of 0x0f with any other bit pattern will retain only the rightmost 4 bits, clearing the left 4 bits.
If the input has a value of 01010001, after doing &0x0F, we'll get 00000001 - which is a pattern we get after clearing the left 4 bits.
Just as another example, this is a code I've used in a project:
Byte verflag = (Byte)(bIsAck & 0x0f) | ((version << 4) & 0xf0). Here I'm combining two values into a single Byte value to save space because it's being used in a packet header structure. bIsAck is a BOOL and version is a Byte whose value is very small. So both these values can be contained in a single Byte variable.
The first nibble in the resultant variable will contain the value of version and the second nibble will contain the value of bIsAck. I can retrieve the values into separate variables at the receiving by doing a 4 bits >> while taking the value of version.
Hope this is somewhere near to what you asked for.
That is doing a bitwise right shift the contents of "input" by 4 bits, then doing a bitwise AND of the result with 0x0F (1101).
What it does depends on the contents and type of "input". Is it an int? A long? A string (which would mean the shift and bitwise AND are being done on a pointer to the first byte).
Google for "c++ bitwise operations" for more details on what's going on under the hood.
Additionally, look at C++ operator precedence because the C/C++ precedence is not exactly the same as in many other languages.
I am new to working with bits & bytes in C++ and I'm looking at some previously developed code and I need some help in understanding what is going on with the code. There is a byte array and populating it with some data and I noticed that the data was being '&' with a 0x0F (Please see code snipped below). I don't really understand what is going on there....if somebody could please explain that, it would be greatly apperciated. Thanks!
//Message Definition
/*
Byte 1: Bit(s) 3:0 = Unused; set to zero
Bit(s) 7:4 = Message ID; set to 10
*/
/*
Byte 2: Bit(s) 3:0 = Unused; set to zero
Bit(s) 7:4 = Acknowledge Message ID; set to 11
*/
//Implementation
BYTE Msg_Arry[2];
int Msg_Id = 10;
int AckMsg_Id = 11;
Msg_Arry[0] = Msg_Id & 0x0F; //MsgID & Unused
Msg_Arry[1] = AckMsg_Id & 0x0F; //AckMsgID & Unused
0x0f is 00001111 in binary. When you perform a bitwise-and (&) with this, it has the effect of masking off the top four bits of the char (because 0 & anything is always 0).
x & 0xF
returns the low four bits of the data.
If you think of the binary representation of x, and use the and operator with 0x0f (00001111 in binary), the top four bits of x will always become zero, and the bottom four bits will become what they were before the operation.
In the given example, it actually does nothing. Msg_Id and AckMsg_Id are both less than 0x0F, and so masking them has no effect here.
However the use of the bitwise-and operator (&) on integer types performs a bit for bit AND between the given operands.