bitwise-operators

Bit-wise operations to implement logical shift to the right [duplicate]

扶醉桌前 提交于 2019-12-20 07:40:26
问题 This question already has answers here : Implementing Logical Right Shift in C (8 answers) Closed 11 months ago . So I am trying to solve this home assignment and I have been stuck with this one particular problem for a couple of hours and can't figure it out. I feel like I am so close! But then i change something in the code and something else isn't right.. /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321

How to perform a union operation in java opencv

心不动则不痛 提交于 2019-12-20 07:08:27
问题 I need to write an equivalent Java OpenCV code as this C++ code Mat1b mask1, mask2; inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1); inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2); Mat1b mask = mask1 | mask2; When i tried to use the | operator it leads to an error. Mat mask1 = new Mat(); Mat mask2 = new Mat(); Core.inRange(hsv, new Scalar(0, 70, 50), new Scalar(10, 255, 255), mask1); Core.inRange(hsv, new Scalar(170, 70, 50), new Scalar(180, 255, 255), mask2);

subtracting two integers bit by bit in assembly

流过昼夜 提交于 2019-12-20 06:34:21
问题 I'm trying to subtract 2 integers bit-by-bit and was given this algorithm b = 0 difference = 0 for i = 0 to (n-1) x = bit i of X y = bit i of Y bit i of difference = x xor y xor b b = ((not x) and y) or ((not x) and b) or (y and b) end for loop I have implemented up to this line b = ((not x) and y) or ((not x) and b) or (y and b) . How should I implement that last line of the algorithm in my code This is what I have so far: INCLUDE Irvine32.inc .data prompt1 BYTE "Enter the first integer: "

Why does if( -8 & 7) return false [duplicate]

荒凉一梦 提交于 2019-12-20 06:25:10
问题 This question already has answers here : Difference between & and && in C? (3 answers) Closed 25 days ago . When i try to run the following code it prints "FALSE" instead of "TRUE" Can somebody explain why the code returns false? #include <stdio.h> int main(void) { if(-8 & 7) { printf("TRUE"); } else { printf("FALSE"); } return 0; } 回答1: -8 can be represented the following way ( I will use a byte representation 8 = 00001000 ~8 = 11110111 -8 = 11111000 (~8 + 1) That is -8 in the two-complement

How to set and clear different bits with a single line of code (C)

夙愿已清 提交于 2019-12-20 03:12:17
问题 data |= (1 << 3) sets bit (3) without disrupting other bits. data &= ~(1 << 4) resets bit (4) without disrupting other bits. How can I accomplish both tasks in a single instruction? (As this is really only for readability, I plan on #define ing this in a cute way like #define gpioHigh(x) <insert code> . The alternative is to figure out how to correctly pass a gpio pointer into functions that I write expressly for this purpose, but eff that) Thanks! Mike 回答1: It's not possible in a single

Does bit shift automatically promote chars to int? [duplicate]

元气小坏坏 提交于 2019-12-20 02:39:07
问题 This question already has answers here : Bitshift and integer promotion? (2 answers) Closed 6 years ago . I read somewhere that bitwise shift automatically turns the operand into an int. But I'm not sure if that statement should be qualified with "if the operands are of unequal type." char one = 1, bitsInType = 8; one << (bitsInType - one); Does the default result of the second line result in an int or char? 回答1: The result type is int in normal C implementations. 1 Per C 2011 (N1570) 6.5.7,

JavaScript | operator [duplicate]

夙愿已清 提交于 2019-12-20 01:40:57
问题 This question already has answers here : Using bitwise OR 0 to floor a number (6 answers) Closed 4 years ago . Anyone able to explain what "|" and the value after does? I know the output for 0 creates sets of 13, the numbers, 3, 2, 1, 0. But what about | 1, or | 2. var i = 52; while(i--) { alert(i/13 | 0); } 回答1: This is a clever way of accomplishing the same effect as: Math.floor(i/13); JavaScript developers seem to be good at these kinds of things :) In JavaScript, all numbers are floating

Can I use bitwise operators instead of logical ones?

有些话、适合烂在心里 提交于 2019-12-19 18:52:26
问题 Bitwise operators work on bits, logical operators evaluate boolean expressions. As long as expressions return bool , why don't we use bitwise operators instead of logical? In this example I use bitwise instead of logical: #include <iostream> int main(){ int age; std::cin >> age; if( (age < 0) | (age > 100) ) // eg: -50: 1 | 0 = 1 std::cout << "Invalid age!" << std::endl; // if( (age < 0) || (age > 100) ) // std::cout << "Invalid age!" << std::endl; return 0; } 回答1: One possible answer is:

C - Algorithm for Bitwise operation on Modulus for number of not a power of 2

六眼飞鱼酱① 提交于 2019-12-19 08:52:07
问题 I know that modulo of power of 2 can be calculated using bitwise operator x % 2^n == x & (2^n - 1). But I am wondering is there any generalized bitwise algorithm exists to find the modulus of any number is not a power of 2. For example, 7%5 Thank you in advance. 回答1: There are a couple, for special cases, including 5. Since 16 ≡ 1 (mod 5), a trick you could do is split your variable into 4-bit nibbles, look up the modulus of each nibble in a table, and add the values together to get the

LC3 Assembly Bitwise Right Shift

前提是你 提交于 2019-12-19 06:56:33
问题 What I need to do it implement both a bitwise left shift, and a bitwise right shift using LC-3 Assembly. Basically, every bit has to be moved over one space in the direction of the shift, and a zero fills the empty space created. Examples: Right Shift: 01001001 00100100→ Left Shift: 01001001 ←10010010 I've successfully implemented a left shift, by taking the binary string, and adding it to itself. I'm stumped on how to perform a right shift. Any thoughts would be greatly appreciated. I have