Questions tagged [bitwise-xor]
Anything related to the bitwise-XOR operation, i.e. a binary operation carried out on two integer operands whose result is obtained performing the logical XOR between each pair of corresponding bits in the operands.
174
questions
0
votes
0
answers
75
views
How to make competitive coding solutions more efficient (BIT wise operations)?
How do I make my code more efficient (in time) pertaining to a competitive coding question (source: codechef starters 73 div 4):
(Problem) Chef has an array A of length N. Chef wants to append a non-...
0
votes
1
answer
46
views
Is the python XOR bitwise operator not just a regular operator?
Other questions on this site suggest that python has no XOR operator, only a bitwise operator ^. But when I try this operator on booleans the result is also a boolean (Python 3.9.12)
True ^ False
>&...
1
vote
1
answer
97
views
Is there a case where bitwise swapping won't work?
At school someday several years ago I had to do a swap function that swaps two integers, I wanted to do this using bitwise operations without using a third variable, so I came up with this:
void swap( ...
1
vote
1
answer
97
views
How do I XOR in a specific spot in my integer?
Could anybody please help me with this? With C I am trying to XOR(^) an int in a specific spot.
These are the steps:
Step 1: Grab and save the leftmost 4 bits before shifting
Step 2: Then I shift left ...
1
vote
0
answers
31
views
Stack Overflow in Recursion for big value of n
How Can I Fix this Stack Overflow for Big value of n? It is working fine for small integers.
I have been attached the question link for reference.
https://codeforces.com/contest/1208/problem/A
Can we ...
1
vote
0
answers
22
views
m1 = 001, m2=101 How can I calculate Bitwise XOR between m1 and m2?
m1 = 001, m2=101 How can I calculate Bitwise XOR between m1 and m2?
Could you check if my solution is correct or not? please tell me your opinions.
m1 = 001 => in 8bit(8byte?) anyway, I can express ...
1
vote
1
answer
69
views
What does the operator ^= do in python? [duplicate]
In the book "Fluent Python" the example 10.11 goes like this:
n = 0
for i in range(1, 6):
n ^= i
1 is the value of n at the end of the loop.
I'd really like to know how this ^= operator ...
1
vote
3
answers
1k
views
A Problem I faced using Bitwise Operators in C (HackerRank)
HackerRank link
In this challenge, you will use logical bitwise operators. All data is stored in its binary representation. The logical operators, and C language, use 1 to represent true and 0 to ...
0
votes
0
answers
10
views
Javascript XOR returning incorrect value of 0 [duplicate]
This xor operation does not behave as expected:
var a = 466483200
var b = 4703528444205332000
console.log(a ^ b)
This returns 0, however clearly these numbers do not have the same bits so I do not ...
-1
votes
2
answers
415
views
Given xor and the range of 2 numbers, find their maximum possible sum
I got this task:
You are given 5 integers a,b,c,d,k. Print the maximum value of x+y
that follows the given conditions:
a<=x<=b
c<=y<=d
x^y=k ('^' sign denotes XOR operation)
...
0
votes
0
answers
252
views
Flipping bits using XOR operator
I'm trying to resolve an easy bit manipulation HackerRank problem using the XOR operator, the problem is as follows:
You will be given a list of 32 bit unsigned integers. Flip all the bits (1 to 0 and ...
0
votes
0
answers
212
views
bitwise XOR in Javascript with a 64 bit integer
I am looking for a way of performing a bitwise XOR on a 64 bit integer in JavaScript.
JavaScript will cast all of its double values into signed 32-bit integers to do the bitwise operations.
I already ...
0
votes
1
answer
106
views
Perform XOR operation on 2 "Long" variables and display the alphanumeric result in .NET Core Console Application
I'm building a .NET Core console application in which I'm trying to perform an "XOR" operation on 2 "Long" datatype values. The result I want should be alphanumeric like "...
0
votes
0
answers
24
views
Shortcut for solving a XOR series
Is there a shortcut formula to compute the following series?
c^(a+r)^(a+2r)^(a+3r)^...^(a+nr)
c, a, n, r > 0
XOR(bitwise) is denoted by ^
Addition(algebric) is denoted by +
-3
votes
1
answer
84
views
Bitwise and doesn't work on unsigned long [closed]
I have this code where I use "bitwise and" (&) to try and change x:
#include <stdio.h>
int main (void){
unsigned long x = 0;
printf("x = %lu\n", x);
x &...
0
votes
1
answer
287
views
How to combine hex value and keep it the same size?
so I pull random 256 bytes each from three different sources in hex value.
x = ...
-1
votes
1
answer
70
views
Why XOR function does not print out the expected hex value?
So I have the following function and I wonder why when I xor X and Y, it does not print out the hex value as expected_result? Is there anything I need to add? Am I missing something in the function?
...
0
votes
1
answer
558
views
How to Xor cipher_text and and hex value?
How do I use python to Xor cipher-text1 XOR cipher-text2 = "3c0d094c1f523808000d09" and "746865" to get this "48656c"? Thanks
It is based on this
https://crypto....
0
votes
0
answers
24
views
Odd behavior of Golang type conversion [duplicate]
For Golang code:
import "fmt"
func main() {
var a uint8 = 1
var b uint8 = 1
fmt.Printf("%T %v %b\n", uint32(a^b)-1, uint32(a^b)-1, uint32(a^b)-1)
fmt.Printf("...
2
votes
1
answer
17
views
Why does the output of this code look like this?
In PHP language,
$var = ("!"^"@");
Why does the value of the line come out as "a?
Demo - https://3v4l.org/b0saN
1
vote
1
answer
67
views
how does the bitwise operator compare the binary in c?
How does the bitwise operator compare the binary?
long num1 = 22; //00010110
long num2 = 21; //00010101
if ((num1 ^ num2) == 0) {
printf("its equal\n");
printf("%li\n", ...
1
vote
1
answer
540
views
When I try to use the exponentiation operator in Rust, it gives strange results. Why?
I am trying to implement Complex-Step Differentiation in Rust. However, when raising the complex number to a power higher than 2, I get a completely different result than Julia. The algorithm works in ...
1
vote
1
answer
105
views
My code passes all my tests but some how edabit is not approving it
function bitwiseAND(n1, n2) {
let a = n1.toString(2).padStart(8, "0");
let b = n2.toString(2).padStart(8, "0");
let x = "";
for (let i = 0; i < 8; i++) {
...
0
votes
1
answer
65
views
Resultant sum of an array when each element is XORed by any constant
Given an array say nums = {1,2,3} and a constant C = 4 , The task is to find the sum of array elements where every element is first XORed by a constant C .
Result : (1⊕ 4) + (2 ⊕ 4) + (3 ⊕ 4) = 18
...
2
votes
1
answer
52
views
How can I run this code without syntax error?
This code gives me a syntax error when I run it on cmd. Any help?
module xor (a, b, c);
input a, b;
output c;
wire c, not_a, not_b;
not a_invert (not_a, a);
not b_invert (not_b, b);
...
0
votes
0
answers
136
views
vector bool compiler xor specialization?
I was thinking again about implementing the quadratic sieve for fun, which requires Guassian elimination over a binary field, that is the operations required are 1. swapping rows and 2. XORing rows.
...
0
votes
0
answers
46
views
Fastest way to check if either one of two numbers is odd using bitwise operations
I came up with the following code (written in Solidity assembly):
let z := xor(and(x, 1), and(y, 1))
Where x and y are of type int256 (they are signed and can have negative values). z is the result, ...
2
votes
2
answers
2k
views
XOR queries on a given array
Given an array of n integers, indexed from 1->n. The task is to perform of Q given queries, and print the sum of the array after each queries.
We can perform three types of operations:
1 X: add X ...
2
votes
3
answers
959
views
Given a list of numbers in which every number appear twice except one. Return that number
I was asked to find out the number from a list of numbers in Python that is present only once inside the list. As usual, I could easily solve it using the normal method that immediately comes out:
...
0
votes
1
answer
119
views
XOR of all the elements of form (start + 2 * i) where i ranges from 0 to n - 1
Background:
This question is actually on leetcode categorized as an easy problem with a solution of having time complexity of O(N).
But I am having hard time understanding solutions by other users of ...
0
votes
0
answers
105
views
Find a subset of length K from a set of N numbers (1...N) where k<=N whose XOR value is X
Write an algorithm to find a subset of length K {A1, A2 ... , AK} from a set of N numbers (1...N) where K<=N and A1^A2..AK-1^Ak is X (where a^b represents bitwise XOR of a and b). Print the subset ...
0
votes
1
answer
432
views
Write a func invert(x,p,n) that returns x with the n bits that begin at position p inverted (1 becomes 0 & vice-versa), leaving the others unchanged
Is the following solution to the question correct?
My solution is:
#include <stdio.h>
unsigned invert(unsigned x, int p, int n)
{
return (x >> (p + 1 - n)) ^ ~(~0 << n);
}
int ...
1
vote
1
answer
347
views
Bitwise Operations in Angular Template Expressions
I am creating a component which represents the days of the week. I am trying eastablish whether one of the days has been selected by using bitwise arithmetic (may be this belongs in coding golf?).
...
1
vote
0
answers
185
views
How to solve bitwise inequalities?
Given two integers L and R. Let’s define XR=L⊕(L+1)⊕(L+2)⊕(L+3)⊕...⊕R (⊕ is bitwise XOR) Is there any integer M such that, (XR⊕M) < XR where L≤M≤R?
Constraint:
1≤ L ≤ R ≤ 10^16
Limits:
1s, 512 MB
I ...
1
vote
2
answers
345
views
XOR on tensors (using vectorization) having float value in Tensorflow
I have two tensors t1 and t2 of the same shape (in my case [64, 64, 3]). I need to compute the XOR of these two tensors. But couldn't figure out a way to do so.
import bitstring
from bitstring import *...
0
votes
0
answers
88
views
Using a bitwise function and then attempting to do the two's complement without success
I'm presently working on a project and I'm at a bit of a stopping point, not knowing if what I'm presently seeing is something that is messing up other parts of my code that follows. In summary, I'm ...
0
votes
2
answers
277
views
Swapping bits in an integer in C, can you explain this function to me?
I want to write a function that receives an unsigned char and swaps between bit 2 and bit 4 and returns the new number.
I am not allowed to use if statement.
So I found this function, among other ...
0
votes
1
answer
208
views
Code giving Run Time Error for the problem Maximum XOR pair in a given Range
I am trying to solve a well known problem called the Maximum XOR pair ina given range. If you are unaware about the probelm here is the prompt given to me:
You are given an array and Q queries of two ...
0
votes
1
answer
1k
views
Searching for a way to do Bitwise xor on images with keys
I am looking for way to encrypt images with keys, first input should be image like this:
Second input look like this:
key = "01010100 01110010 10101110 01110001 10110000 01100001 01010010 ...
-3
votes
1
answer
725
views
We are missing some condition for n>2, can someone help to find special cases which our code is missing?
Problem Statement:
You are given a sequence A1, A2,…, AN and you have to perform the following operation exactly X times:
Choose two integers i and j such that 1≤i<j≤N.
Choose a non-negative ...
0
votes
1
answer
349
views
compiler warning "warning: iteration 10u invokes undefined behavior [-Waggressive-loop-optimizations]" for M[i] ^ k;
Consider the following code at http://cpp.sh/9afy:
#include <iostream>
#include <iomanip>
uint8_t S[256];
uint8_t T[256];
uint8_t K[] = {0};
const int k_len = 1;
uint8_t M[] = {0,0,0,0,...
0
votes
1
answer
83
views
What is the meaning of XOR sum in this paper?
In this formulation: j is an aggregator node in wireless sensor network, $ B_{j}$ is the final aggregated result from the right side, $B_{i}^{'}$ is the data that send by sensor node i. According to ...
2
votes
2
answers
1k
views
When bitwise and & operation is greater or equal to bitwise xor ^ operation?
When bitwise & operation is greater or equal to bitwise ^ operation?
For example, 4&7 >= 4^7 is true.
How do we know when it will be true?
The one way is to do manually what will be hidden ...
3
votes
1
answer
106
views
Bit operators C++
Can someone explain to me how this function works? I understand the drop the lowest set of a bit of x part which is the 5th line but I don't understand the assignment result ^= 1; Shouldn't result be ...
0
votes
1
answer
145
views
Can this code be further optimized so that I do not get Time Limit Exceeded or TLE on competitive website?
The snapshot of the question
ON a competitive coding website, I came across this question. I was able to solve it for the basic use cases, using the below code. However, the time limit exceeded for ...
2
votes
2
answers
798
views
Apply Bitwise operator on multiple integers at once
I have a list of integers, that can have any number of items. Now, I want to calculate the BITWISE XOR of all these numbers. If the numbers are already known, this can be done as follows:
int xor = 10 ...
1
vote
1
answer
150
views
bitParity checker - count how many 0's in a bit vector
I was looking into bitwise operators and bit manipulation and stumbled upon this thread: bitParity - Finding odd number of bits in an integer
To summarize, the user asked for a function, int bitParity(...
0
votes
1
answer
191
views
C program for xor operation, operators entered by scanf [duplicate]
I wrote a small program to run a bitwise XOR operation. The values shall be inserted in the command line:
#include <stdio.h>
int main()
{
unsigned char x = 0, y= 0;
printf("This ...
0
votes
1
answer
208
views
Why XOR of all subsets of a list have same frequency?
I have a list of numbers a1, a2, a3, a4, a5, ... and so on. If we find XOR of all subsets then I noticed that frequency of each distinct XOR is the same.
Example 1
# list of numbers
[2, 0, 9]
#...
3
votes
1
answer
958
views
Can xor of distinct prime numbers be 0?
I have tried this exercise for a few sets e.g. {2, 3, 5}, {5, 11} where xor of elements is not 0. My intuition suggests that it will always be non-zero but I am unable to prove it. I searched on the ...