Questions tagged [bitwise-and]

Anything related to the bitwise-AND operation, i.e. a binary operation carried out on two integer operands whose result is obtained performing the logical AND between each pair of corresponding bits in the operands.

Anything related to the bitwise-AND operation, i.e. a binary operation carried out on two integer operands whose result is obtained performing the logical AND between each pair of corresponding bits in the operands.

317 questions
122
votes
4 answers

What does value & 0xff do in Java?

I have the following Java code: byte value = 0xfe; // corresponds to -2 (signed) and 254 (unsigned) int result = value & 0xff; The result is 254 when printed, but I have no idea how this code works. If the & operator is simply bitwise, then why…
dagronlund
  • 1,612
  • 3
  • 13
  • 15
77
votes
4 answers

How do I use Java's bitwise operators in Kotlin?

Java has binary-or | and binary-and & operators: int a = 5 | 10; int b = 5 & 10; They do not seem to work in Kotlin: val a = 5 | 10; val b = 5 & 10; How do I use Java's bitwise operators in Kotlin?
Water Magical
  • 979
  • 1
  • 9
  • 13
72
votes
5 answers

What is (x & 1) and (x >>= 1)?

I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer…
Sandra K
  • 1,209
  • 1
  • 10
  • 21
66
votes
3 answers

What does (number & -number) mean in bit programming?

For example: int get(int i) { int res = 0; while (i) { res = (res + tree[i]) % MOD; i -= ( (i) & (-i) ); } return res; } A tree update function: void update(int i, int val) { while (i <= m) { tree[i] =…
SwadhIn
  • 717
  • 1
  • 9
  • 19
49
votes
5 answers

Mod of power 2 on bitwise operators?

How does mod of power of 2 work on only lower order bits of a binary number (1011000111011010)? What is this number mod 2 to power 0, 2 to power 4? What does power of 2 have to do with the modulo operator? Does it hold a special property? Can…
Zo Has
  • 12,599
  • 22
  • 87
  • 149
31
votes
7 answers

Bit wise '&' with signed vs unsigned operand

I faced an interesting scenario in which I got different results depending on the right operand type, and I can't really understand the reason for it. Here is the minimal code: #include #include int main() { uint16_t check…
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
28
votes
7 answers

explain arguments meaning in res = cv2.bitwise_and(img,img,mask = mask)

I am trying to extract blue colour of an input image. For that I create a blue HSV colour boundary and threshold HSV image by using the command mask_img = cv2.inRange(hsv, lower_blue, upper_blue) After that I used a bitwise_and on the input image…
SACHIN
  • 301
  • 1
  • 3
  • 4
27
votes
3 answers

Doing a bitwise operation on bytes

I got two objects, a and b, each containing a single byte in a bytes object. I am trying to do a bitwise operation on this to get the two most significant bits (big-endian, so to the left). a = sock.recv(1) b = b'\xc0' c = a & b However, it angrily…
Chloride Cull
  • 741
  • 1
  • 7
  • 16
21
votes
3 answers

bitwise-ANDing with 0xff is important?

Doesn't bitwise-ANDing with 0xff essentially mean getting the same value back, for that matter, in this code? byte[] packet = reader.readPacket(); short sh; sh = packet[1]; sh &= 0xFF; System.out.print(sh+" "); Weirdly, I get a -1 if that ANDing is…
17
votes
8 answers

Bitwise AND on 32-bit Integer

How do you perform a bitwise AND operation on two 32-bit integers in C#? Related: Most common C# bitwise operations.
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
16
votes
3 answers

BITWISE AND(&) for Range of Numbers

Given two numbers L & R , Find Bitwise AND of all numbers lying between L and R inclusive Constraints 1<= L,R <= (2^32). LL step = 1; while(L!=R) { L/=2; R/=2; step*=2; } cout<
naveensangtani
  • 163
  • 1
  • 1
  • 7
16
votes
8 answers

Meaning of bitwise and(&) of a positive and negative number?

Can anyone help what n&-n means?? And what is the significance of it.
Shubham Tyagi
  • 163
  • 1
  • 1
  • 4
13
votes
11 answers

XOR from only OR and AND

How do you do the XOR bitwise operation if you only have available the AND and the OR operations?
John Zane
  • 878
  • 1
  • 9
  • 22
11
votes
1 answer

java & operator with two integers?

From what I understand the & operator is similar to the && operator except that the && only checks the second if the first is true, while the & checks both regardless of the result of the first one. Basically the && just saves a little time and…
nife552
  • 213
  • 1
  • 2
  • 8
9
votes
1 answer

Translating bitwise comparison from C++ to C#

I've the given condition from a cpp source. if (!(faces & activeFace) || [...]) { ... } I want to translate this into C#. When I understand this right, this means as much as if activeFace is *not* in faces then... - not? So what would be the…
boop
  • 7,413
  • 13
  • 50
  • 94
1
2 3
21 22