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
8
votes
6 answers

Why do a bitwise-and of a character with 0xff?

I am reading some code that implements a simple parser. A function named scan breaks up a line into tokens. scan has a static variable bp that is assigned the line to be tokenized. Following the assignment, the whitespace is skipped over. See below.…
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
8
votes
2 answers

Significance of x & (-x) in 2's Complement?

Where '-' denotes negative x, and '&' denotes bitwise AND. The numbers are in 8-bit 2's complement in a program and I can't seem to find the correlation between inputs and outputs. 8 & (-8) = 8 7 & (-7) = 1 97 & (-97) = 1 So possibly the…
ThoseKind
  • 754
  • 2
  • 13
  • 22
8
votes
2 answers

Why the ArrayDeque class use bitwise operation in the pollFirst method?

I look through java source code try to learn the implementation of collection. Found a interesting thing in the ArrayDeque class. public E pollFirst() { int h = head; @SuppressWarnings("unchecked") E result = (E) elements[h]; //…
Albert Gao
  • 3,653
  • 6
  • 40
  • 69
8
votes
2 answers

Test against odd numbers

Most commanly the modulo operator % is used to test against an even or odd Number. Now my question is, is there any Problem testing against an odd number using a bitwise AND, as it feels much more natural testing whether the rightmost bit is 1 or 0…
Moritz Roessler
  • 8,542
  • 26
  • 51
6
votes
1 answer

Is there any reason to use (nr & 1 == 0) over (nr % 2 == 0) to check for parity?

Is there any actual difference in performance? Is it faster? (lets say I use it in at least 100 cases in the same program, would it improve my program in terms of speed?)
Stephan
  • 69
  • 6
6
votes
4 answers

Can I assume that a bitwise AND operation is O(1)? If so why?

My processor can only do arithmetic operations on 8-bit or 16-bit unsigned integers. 1) This means the word size for this processor is 16 bits, correct? Operations on words are O(1). The reason for this has to do with the actual circuit-level…
evianpring
  • 3,316
  • 1
  • 25
  • 54
6
votes
3 answers

Bitwise & in javascript not returning the expected result

I am working on BitWise AND operator in javascript. I have two 32 bit nunber 4294901760 (11111111 11111111 00000000 00000000) and 4294967040 (11111111 11111111 11111111 00000000) when I and them bitwise 4294901760 & 4294967040 I got -65536 as a…
user2243651
  • 325
  • 5
  • 13
6
votes
3 answers

What does the bitwise code "$n & ($n - 1)" do?

What does this code mean and what are other ways accomplish the same without using bit shifting? if ($n & ($n - 1))
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
5
votes
3 answers

Produce solutions of k & x = k in range (0,n)

How to efficiently generate all numbers within 0,1,2...n. (large n). Such that for a fixed x and varying k (0 <= k < n), k & x = k. It is easily found out that all the bits with value 1 in k is also 1 in x. But I have trouble computing all of…
5
votes
1 answer

What is the function of bitwise & in this statement?

I have been reading about an implementation of the diamond-square algorithm in C# that wraps around to create seamless textures. To calculate the next point, an average is taken of four sample points arranged in a square or a diamond. If a sample…
camarones95
  • 165
  • 4
5
votes
2 answers

Bitwise operations - checking and removal

Note the simple example below: Module Module1 Public Enum Names None = 0 Test = 1 Test2 = 2 Test3 = 4 Test4 = 8 End Enum Sub Main() Dim test As Names = Names.Test…
Interminable
  • 1,338
  • 3
  • 21
  • 52
5
votes
4 answers

"Bitwise And" and Left-Padding in C++

I have a macro that looks something like this: Foo(x) ((x - '!') & 070) If I call the following code: Foo('1') => 16 However, if I call the following code: (('1' - '!') & 70) => 0 So my question is, what's going on here? Why does x & 070 compute…
sohum
  • 3,207
  • 2
  • 39
  • 63
5
votes
7 answers

Mask and extract bits in C

I've been looking at posts about masks, but I still can't get my head around how to extract certain bits from a number in C. Say if we have an integer number, 0001 1010 0100 1011, its hexadecimal representation is 0x1A4B, right? If I want to know…
5
votes
1 answer

Bitwise operations in MS access

I have a table which contains an answer. This answer is a combination of a set list of numbers. (see below) Possible values 2 4 8 16 32 64 If I have the answer of Answer 24 – I need some method to work out the only values that could…
Symon Turner
  • 155
  • 2
  • 11
5
votes
4 answers

bitwise AND in java with operator "&"

I just read this code following: byte[] bts = {8, 0, 0, 0}; if ((bts[i] & 0x01) == 0x01) Does this do the same thing as if (bts[i] == 0x01) If not,what's the difference between them? And what is the first way trying to do here?
Johnny Chen
  • 2,025
  • 1
  • 18
  • 27
1
2
3
21 22