Questions tagged [bit-manipulation]
96 questions
4
votes
3 answers
Calculating XOR of all numbers from 1 to n: Why does this method work?
Given a number n, the task is to find the XOR of every number from 1 to n. Why does the following algorithm work?
Find the remainder of n by moduling it with 4.
If rem = 0, then XOR will be same as n.
If rem = 1, then XOR will be 1.
If rem = 2,…
MonkaS
- 89
- 1
- 2
4
votes
2 answers
Converting a number from N to 0 in binary
Trying to solve this problem since 2 days. Still unable to figure out even a basic approach.
Given a number $N$ in binary ($1$ to $10^5$), we need to convert it to $0$ using only 2 operations. Given a binary value of a number with length $M$ ($0$…
Aditya Singh
- 41
- 2
3
votes
1 answer
How to distinguish between bits?
I read a post about differences between synchronous and asynchronous transmission modes.
That post has a picture about how bits flows from terminal to computer in async mode.
But how computer can distinguish between 1s and determine what 1 does…
Kenenbek Arzymatov
- 415
- 3
- 8
3
votes
0 answers
Bridging inductive natural number and bits?
Most popular representation for the natural numbers in type systems is:
Inductive nat : Set :=
| 0 : nat
| S : nat -> nat.
However, digital computers usually represent numbers as bit sequences, arranged into bytes.
The above inductive…
Roman Susi
- 283
- 1
- 8
3
votes
4 answers
How to find the largest power of two divisor of a given integer?
Given an integer $x$, I need to find the largest power of two $p = 2^n$ that divides $x$ such that the remainder is zero.
When $x$ is zero, the algorithm should return zero. For odd numbers, it should return one, since the largest power of two that…
Paul Razvan Berg
- 184
- 2
- 11
3
votes
2 answers
Minimum XOR for queries
I was asked the following question in an interview.
Given an array $A$ with $n$ integers and an array $B$ with $m$ integers. For each integer $b \in B$ return and integer $a \in A$ such that $a \otimes b$ is minimum, where $x\otimes y$ for to…
Sam Si
- 133
- 5
3
votes
1 answer
Evaluating predicate on binary strings
Consider two unknown binary strings $$X = x_{1} x_{2} \dots x_{n^{2}}, \quad Y = y_{1} y_{2} \dots y_{n^{2}}, \quad x_{i}, y_{i} \in \{0, 1\} .$$ We may request a string $Z = z_{1} z_{2} \dots z_{n^{2}}$, where $z_{i} = x_{i}$ or $z_{i} = y_{i}$, no…
matroid1998
- 39
- 2
3
votes
1 answer
Is is possible to determine if a given number is xor combination of some numbers?
I have been given a number Y which is ($a$ xor $b$ xor $c$ xor $d$ xor $e$ ) of some numbers ($a$,$b$,$c$,$d$,$e$) and another no X. Now i have to determine if X is a xor combination of ($a$,$b$,$c$,$d$,$e$)
e.g - ($a$ xor $d$) , ($b$ xor $c$ xor…
foobar
- 31
- 3
3
votes
2 answers
Find the bitwise AND of a subarray closest to given number
It was a question I was asked in an online assessment for a company.
Please help me in this question-
Given an array, $A$ of size $N$ and an integer $P$ , find the subarray $B = A[i \dots j]$ such that $i \leq j$ and compute the bitwise value of…
akshay sharma
- 39
- 2
2
votes
1 answer
How are the prime numbers encoded in Knuth's example of fitting primes into memory cache?
Could somebody please help me understand what is going on here (in plain English)? I think that $(k \mathbin{\&} 63)$ has the effect of modular division. Is that right? How are the primes encoded / how can I know exactly where a number starts and…
Jonathan Komar
- 145
- 5
2
votes
1 answer
Difference between CNOT and 2nd bit bitflip
I understand how the outcome of each is supposed to be different, but in matrix form are these gates not the same? The CNOT matrix negates the second bit regardless of the input of the first.
Sam J
- 21
- 1
2
votes
1 answer
For given set of integers, find and count the pairs with maximum value of bitwise or
Let's say we have given set of integers $A = \{x_1, x_2, x_3, x_4, \dots, x_n\}$, consisting of exactly $n$ values, all of them positive integers. Now the problem is to find the maximum value of bitwise or operation of all possible pairs $(x_i,…
someone12321
- 1,428
- 15
- 27
2
votes
1 answer
Maximize AND on a sequence of XORs
Problem
We are given 2 arrays a and b both of length n. We build a third array c by rearranging the values in b. The goal is to find the optimal c that maximizes
result = (a[0] ^ c[0]) & (a[1] ^ c[1]) & ... & (a[n - 1] ^ c[n - 1])
where ^ is XOR…
Kolya Ivanov
- 23
- 3
2
votes
1 answer
Compressing a bit string when I know how many 1s and 0s there are
Say I have a 256 bit bit-string, and I know that there are 16 ones and 240 zeros. I know that this bit string can be compressed, because there are only 256 choose 16 possible strings that satisfy this condition: that's about 2^83.06.
So I know that…
Reggie Simmons
- 133
- 4
2
votes
1 answer
Is the following Greedy algorithm to generate Gray Codes always correct?
I recently solved the basic problem of generating a n-bit Gray Code. The solution I used involved building larger-bit Gray Codes from smaller ones recursively (the solution I've seen on most websites).
However, I then had an idea for a Greedy Gray…
Christopher Miller
- 327
- 2
- 12