Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am examining a C code for a pressed key in a embedded system.
In the code below you can see an if-statement with ! before checking REG8, what does this mean in a situation like this? I am just asking about the character (!) and not what the code does.
if(!(REG8(DataRegA) & 0x80)){
*key=REG8(DataRegA) & 0x0F;
return(1);
}
that means the button is active on low better known as active low
That code checks the state of the 7th bit in that register; if it's off, it executes the block of code.
REG8(DataRegA) presumably gets the value of some MCU register, which probably reflects the state of some input signal;
REG8(DataRegA) & 0x80 performs a bitwise AND with 0x80, which returns 0 if the seventh bit is not set, 0x80 otherwise;
the ! is the logical negation operator; in !(REG8(DataRegA) & 0x80) it negates the expression above, i.e. if it's 0 it becomes 1, if it's nonzero it becomes 0.
Thus, the if body is executed only if the 7th bit in the register is not set.
The ! operator yields 0 if its operand is nonzero and 1 if its operand is 0, and the if-statement executes its body if the condition is nonzero. So if REG8(DataRegA) & 0x80 evaluates to 0 (which would make the code in the if-statement's block not execute), the ! operator will reverse it and make it execute.
! is the boolean not operator. So if <expr> is true (i.e. non-zero), then !(<expr>) is false (zero), and vice versa.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to merge two characters and print them via a single variable by using ASCII (refer to the image below):
[1]: https://i.stack.imgur.com/TWodP.jpg
try this if your machine is little endian
unsigned int C = (b << 8) | a;
printf("%s",&C);
otherwise if your machine is big endian try
unsigned int C = (a << 24) | (b<<16);
printf("%s",&C);
Based on my comment, but improved by Jonathan's input, I propose to do this:
int C;
C= (((unsigned char)a)<<8)|((unsigned char)b);
You have already tried the commented version to be helpful, this one is basically the same, just being robust against potentially negative values of a and b (which I considered out of scope, but Jonathan is right in being as safe as possible).
As for the explanation:
The << 8 part, a so-called bitshift left, moves a value by 8 bit towards the MSBs.
I.e. a 00000000010000001 becomes a 01000000100000000.
To be safe from negative value (see below why that is important), the value is first type-casted to unsigned char. That is the part ((unsigned char)a). Note that I tend to be generous when it comes to using (), some people do not like that. This is done for both values.
With values 'A' and 'B' we end up with
0100000100000000 and
0000000001000010.
The next part uses a bitwise OR (|), in contrast to a logical OR (||).
The result is
0100000101000010, which is what I understand to be your goal.
The importance of protecting against negative input is this. Any negative 8bit value has the MSB set and when cast to a wider data type will end up with all 8 new high bits set. This is because of the representation of negative values in integers in 2-compliment.
The final conversion to the desired wider data type is as Jonathan explains:
If you have (unsigned char)A << 8 as a term, then the unsigned char value is extended to int before the shift occurs, and the result is an int.
This question already has answers here:
Understanding the bitwise AND Operator
(4 answers)
Closed 4 years ago.
I'm new to programming and have a question I was hoping I could get some help with.
I have a binary value 0100 0001 0000 0001 which has been assigned to a variable name valhex. I'm supposed to use the bitwise AND operator to make bits 13 through 3 KEEP their current value and ALL other bits are set to zero, then store the result back into the variable valhex. I'm supposed to do this using only one line of C code.
So far all I have is this:
unsigned int valhex = valhex&0000000100000000;
I know this isn't right but this is as far as I can get. I don't know where to put the & symbol in relation to the variable and the binary. I'm also not sure if I'm doing the right thing by making bits 0,1,2,14,15 zeroes. I thank you in advance for any help you might be able to give me.
In bitwise AND (if you recall your truth tables), bits that are ANDed with 1 keep their value, bits that are ANDed with a 0, are set to 0. So if you want to keep bits 13-3, your mask needs to have 1s in position 13-3, and 0s in position 2-0. Also note that to specify a binary literal, you need to prefix it with 0b. Also note that you can not declare and use the variable on the same line because it's uninitialized. The end result is this:
unsigned int valhex = 12345; /* some value */
valhex = valhex & 0x3ff8; /* 0x3ff8 = 0b11111111111000 */
Note that unsigned int is longer than 14 bits and you didn't specify what is supposed to happen to bits in position 14 and up. In this case, they'll be set to 0s as well.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Suppose you have code including:
if(i & (-1)) {}
Depending on i, what would this operation return?
There's no definitive answer to this question: it depends on the type of i and, if the operation is performed in the domain of signed type, on the signed representation used by the given platform.
For example, if i is of type unsigned int (or some larger unsigned type), the entire operation will be performed in the domain of that unsigned type. In that case -1 will get implicitly converted (by usual arithmetic conversions) to all-ones bit pattern as wide as i. The whole if will effectively become equivalent to if (i).
But with i of signed type - there's no way to say anyhting for certain.
The results of performing a bitwise operation on a negative value are implementation defined.
For example, if 2's complement representation is used for negatives, the value -1 will be represented by a sequence of all 1 bits, so performing a bitwise AND with -1 will result in the value of i.
On the other hand, if sign magnitude representation is used, only 2 bits are set in the value -1, the highest and the lowest. In that case, only the highest and lowest bits of i (after any conversions) will be set in the result.
So to summarize, you can't depend on the results without some implementation defined method of determining the representation of negative values.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For the below sample code:
// Note that a, b, c and d can have value of 0 or 1 only
int isAllTrue(int a, int b, int c, int d)
{
return (a && b && c && d);
// THIS ALSO RETURNS CORRECT VALUE
// return (a & b & c & d);
}
If we know that operands can be either 0 or 1, which operation would be preferable from performance point of view: bitwise or logical? Does it really matter?
Does evaluation stop in the case of bitwise "&" when the operand is 0 ?
Use & for bit operations. Use && for logical operations. If you use a bitwise operation where you should use a logical operation or vice versa, the reader will (a) be confused, (b) check very carefully if your code has introduced a subtle bug.
Programming relies on idioms. If your code doesn't match the expected idiom, then you must have a good reason, and you should write a comment explaining why.
It doesn't matter which formulation you use, with active compiler optimization, all shown evaluations should already be completed at compile time.
As for your last question, the bitwise operators do not employ shortcut evaluation and thus always evaluate each operand.
there seems to be a lot of confusion about questions like this one ... and a lot of wrong answers. Well, here are the facts for C/C++ :
&&, || and ! are logical operators, they test for
boolean expressions - integral values (numbers) are considered true if their value is not zero, false otherwise -
no matter how many numbers you check for but 0 && 1 will be evaluated to false, for instance because of the first 0 -- it could be re-written as false && true
&, |, ~, ^, >>, << are bitwise operators, they mask/assign
bit patterns of integral values, you can do a lot of cool calculations like that - but you really need to know how numbers are represented inside of your CPU before you start doing that.
Conclusion : your question doesnt make sense - because you thought that these operators are similar .... they arent.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
According to text:
"In rare case of C/C++ compiler that does not perform sign extension on right shift of a negative number, the trick to shift right to divide the number fails"
Consider the following example:
unsigned int i = 0b10000000; // 128
i = i >> 1; // i equals 01000000 i.e. 64
That is the only way I know to block sign extension. Also, by adding unsigned it becomes a positive number so please correct.
Sign extension on bitwise right-shift of negative number is implementation-defined in C. It means it is up to the compiler to decide if it performs the sign propagation and the compiler documentation has to document the selected behavior.
From the C Standard:
(C99, 6.5.7) "If E1 has a signed type and a negative value, the resulting value is implementation-defined."
Among compilers, gcc always propagates the sign:
Signed `>>' acts on negative numbers by sign extension.
http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html