bitwise-operators

bit shift multiplication in c not using powers of 2 [duplicate]

China☆狼群 提交于 2019-12-24 03:51:40
问题 This question already has answers here : How can I multiply and divide using only bit shifting and adding? (13 answers) Closed 6 years ago . How can I perform multiplication by 36 using bit-shifting? Isn't it only possible to multiply by powers of 2? For example: unsigned x = 4; // binary 00000000 00000000 00000000 00001000 unsigned y = x << 3; // multiply by 8, resulting in binary 00000000 ... 00100000 Thanks! 回答1: You can't multiply by a non-power of 2 by bit shifting alone. But you can

warning : 'integer conversion results in truncation'

自作多情 提交于 2019-12-24 02:05:40
问题 I get an warning here. The warning says 'integer conversion results in truncation'. It persists even if I remove the typecast(U16). typedef unsigned short U16; U16 mask; mask = ~(U16)(0x8000); How do I resolve this warning? I used the below code and removed the warning, but unsure if its the right way to do it. mask = (U16)(~(U32)(0x8000)); Thanks in advance! 回答1: C compilers don't like when you try to assign constant values into an L-value that's not big enough to hold them. I would guess

Why do certain situations require the use of 'bitwise' operators instead of 'logical' / 'equality' operators?

喜夏-厌秋 提交于 2019-12-23 20:58:59
问题 The other day I was trying to code a small C++ programming using the SDL multimedia library, and I ran into this small snag which I eventually solved through trial and error. The issue is, I understand what I did to solve the problem, but I don't really understand the nature of the problem! The issue was with keyboard event handling in SDL. The code to handle a single key press to exit the program is straight forward and simple. [eventQueue is an SDL_Event structure] //checks for keypress

How to bitwise operate on memory block (C++)

我的梦境 提交于 2019-12-23 16:14:23
问题 Is there a better (faster/more efficient) way to perform a bitwise operation on a large memory block than using a for loop? After looking it to options I noticed that std has a member std::bitset , and was also wondering if it would be better (or even possible) to convert a large region of memory into a bitset without changing its values, then perform the operations, and then switch its type back to normal? Edit / update: I think union might apply here, such that the memory block is allocated

Java shift operator

感情迁移 提交于 2019-12-23 14:39:00
问题 Consider the following Java code: byte a = -64; System.out.println(a << 1); The output of this code is -128 I tried as follows to figure out why this is the output: 64 = 0 1000000 (the MSB is the sign bit) -64= 1 1000000 (Tow's complement format) Expected output after shifting: 1 0000000 (This is equal to 0, because the MSB is just a sign bit) Please anyone explain what I am missing. 回答1: The two's complement representation of -128 is 10000000, thus your results are correct. 回答2: 10000000 is

Bitwise operation on longs

妖精的绣舞 提交于 2019-12-23 13:11:35
问题 I trying to compile this code: Int64 itag = BitConverter.ToInt64(temp, 0); itag &= 0xFFFFFFFFFFFFFC00; However this gives me the following error: Operator '&=' cannot be applied to operands of type 'long' and 'ulong' How do I do this? 回答1: See http://msdn.microsoft.com/en-en/library/aa664674%28v=vs.71%29.aspx . If the literal has no suffix, it has the first of these types in which its value can be represented: int , uint , long , ulong . You have 0xFFFFFFFFFFFFFC00 but Int64.Max is:

Can parentheses in C change the result type of operands of a bitwise operation?

流过昼夜 提交于 2019-12-23 09:59:38
问题 I have fed the following code through a static analysis tool: u1 = (u1 ^ u2); // OK u1 = (u1 ^ u2) & u3; // NOT OK u1 = (u1 ^ u2) & 10; // NOT OK u1 = (u1 ^ u2) & 10U; // NOT OK u1 = (unsigned char)(u1 ^ u2) & 10U; // OK u1 = (unsigned char)(u1 ^ u2) & u3; // OK "OK" means the static analysis tool did not complain. "NOT OK" means the static analysis tool did complain -- claiming that some operand of a bitwise operation is not an unsigned integer. The results from the last 2 lines show that

Why hasn't my variable changed after applying a bit-shift operator to it?

久未见 提交于 2019-12-23 09:06:53
问题 int main() { int i=3; (i << 1); cout << i; //Prints 3 } I expected to get 6 because of shifting left one bit. Why does it not work? 回答1: Because the bit shift operators return a value. You want this: #include <iostream> int main() { int i = 3; i = i << 1; std::cout << i; } The shift operators don't shift "in place". You might be thinking of the other version. If they did, like a lot of other C++ binary operators, then we'd have very bad things happen. i <<= 1; int a = 3; int b = 2; a + b; //

Shift operators in PL/SQL

∥☆過路亽.° 提交于 2019-12-23 08:30:07
问题 Whether there is an alternative of shift operators in PL/SQL? There is bitand function, but it accepts only binary_integer -type arguments. What should I do if I need check up lower/higher bit of really long number (probably set in the line)? In C there are << and >> operators. How I can realise them in PL/SQL? 回答1: The following answer is not endianness agnostic and my wording is based on little endian format... You can shift bits simply multiplying (shift left) or dividing (shift right) the

Shift operators in PL/SQL

混江龙づ霸主 提交于 2019-12-23 08:29:19
问题 Whether there is an alternative of shift operators in PL/SQL? There is bitand function, but it accepts only binary_integer -type arguments. What should I do if I need check up lower/higher bit of really long number (probably set in the line)? In C there are << and >> operators. How I can realise them in PL/SQL? 回答1: The following answer is not endianness agnostic and my wording is based on little endian format... You can shift bits simply multiplying (shift left) or dividing (shift right) the