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.
289
questions
0
votes
1
answer
27
views
Discord permissions fields in PHP
I have to check permissions for each guild of the current logged in user.
I tried this but it does not works because PHP convert string to int, and if it is too large, it will not be the right number.
...
0
votes
1
answer
26
views
SQL Server custom CLR aggregate bitwise and
I wrote a custom CLR bitwise-and aggregate function in C#. But it is not working.
It always returns value of the last row in aggregated records.
What's the mistake?
[Serializable]
[Microsoft.SqlServer....
0
votes
1
answer
72
views
Is there a solution for a bitwise logical AND (&) operator between two Enum values in C#
Consider the following simple Flags Enum in C#:
[Flags]
public enum CountingEnum
{
Zero = 0,
One = 1 << 0,
Two = 1 << 1,
Three = Two | One,
Four = 1 << 2,
...
0
votes
1
answer
49
views
How to convert one byte and 4 bits from another byte to a short using bitwise?
Using bitwise, how could we convert these 3 bytes to two shorts in this pattern in the most performant way?:
(11111111)(01111110)(10000001) 3 bytes
(111111110111)(111010000001) 2 shorts
Found a way to ...
1
vote
3
answers
150
views
Difference between !(n & 1) and n & 1 == 0 in C++
For some reason in C++, the expressions if(!(n & 1)) and if(n & 1 == 0) seem to not be equivalent.
Can someone please explain why this happens?
2
votes
1
answer
38
views
Bitwise & operator in Snowflake?
I'm stuck with Snowflake about bitwise operators. In MySql it's allowed to use this syntax:
Flag & 1024 > 0
I'd like to make the same filtering in the where clause with Snowflake but I've ...
0
votes
1
answer
52
views
Bit flags operation
I am going through some format spec and I am encountering the following:
if (flags & 1) {
...
}
Now, according to the same documentation flags is:
so the flags is 3 bytes. The operation flags &...
-2
votes
1
answer
28
views
why I am getting 'NaN' for '&' operator?
I am not able to understand why I am getting this "NaN" while performing AND-Bitwise operator for some numbers only? please see pic I attached to understand My issue clearly.
'use strict'...
3
votes
2
answers
77
views
bitand : keyword vs function in C++
I've tried using the alternative bitwise operator 'bitand' in below simple code. Its appears that I can use bitand as a keyword as well as a function in Visual C++, both yielding different results, ...
1
vote
1
answer
66
views
Bitwise AND for all integers in tuple/list
I have the following function, where v is a tuple of integers and hits is an integer. The function uses bitmasks to first check that no set bits are shared by any two integers in the tuple and then ...
-1
votes
1
answer
31
views
Condition checking gives wrong answer
#include <stdio.h>
int main(){
printf("%d,%d\n", 2 & (1<<1) , 2 & (1<<1)>0 );
return 0;
}
the output of this program is 2,0.
2 & (1<<1) is equal ...
-3
votes
1
answer
92
views
Java equivalent of & (single ampersand) in if statement, like in C?
So after learning both C and Java, Java doesn't have the capability of Bitwise-Anding in an if-statement between two values.
int x = 1011;
int y = 0110;
// 0010
if (x % y) {
printf("...
-3
votes
1
answer
55
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
0
answers
58
views
cv2.bitwise_and sizes of input arrangements are not match error
I am taking this error (-209:Sizes of input arguments do not match). How can I fix this?
import cv2
import numpy as np
image = cv2.imread("foto2.png")
image2 = cv2.imread("foto3.png&...
1
vote
1
answer
52
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++) {
...
-1
votes
1
answer
66
views
What does a bitwise AND do with no value infront of it?
I am using Verilog. Say you have the term:
& A
or
~& A
What does this do? Does it just compare it to an all empty array?
1
vote
0
answers
42
views
Count number of binary intersections (AND) for each element with other elements of array
While solving another complicated problem, I've got this one: I have array of longs, for example, in a binary representation it would look like:
0000100
0000100
0000101
0000001
...
1
vote
3
answers
133
views
What to use - modulus or bitwise AND operator when creating an implementation of a Hashmap in Java?
I have a university assignment in which we are to create our own HashMap implementation. In the assignment, when calculating the index of an entry to be placed in the array, it says to use the modulus ...
-3
votes
3
answers
66
views
What is the meaning of this operation [closed]
So we got this piece of code on a test and I had zero idea what is the meaning and how it works.
unsigned int a = 1, b = 2, c;
and then c = a&b || a&&b;
The question was: What is the value ...
0
votes
0
answers
14
views
PrestoDB equivalent bitwiseAND function in SparkSQL
I have a prestoDB function which validates (case when (bitwiseAND("col", 1) <> 0) then 1 else 0 end)
What is the equivalent function in SPark SQL for above ? I see there is bit_and(...
0
votes
1
answer
108
views
Understanding bitwise binary combination algorithm
I've been looking at algorithms that list all the possible combinations of an array of boolean values.
For example, an array of two booleans can have these combinations:
[true, true], [true, false], [...
0
votes
0
answers
38
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, ...
0
votes
2
answers
66
views
Why Bitwise AND operation not working for Value>=8?
This program is to check whether a given number is a power of 2. This code works properly till n=7 and it gives irrelevant output for input n<=8.
Input n = 4;
Output : 0 ;
Expected output : 0
Input ...
3
votes
0
answers
136
views
Is x & 1 always a correct way to determine if a number is odd?
I've heard from many answers that x & 1 == 1 will determine if a number is odd (which makes sense); but I never see this in practice (x % 2 != 0 is usually used instead)
Are there any potential ...
1
vote
2
answers
45
views
Behaviour of bitwise AND and logical AND along with equality operator in Python 3
So I understand bitwise AND is not always safe to use especially if the operands are going to numbers. But am trying to understand what is that tiny subtle reason behind this statement returns to ...
1
vote
0
answers
32
views
Apply bitwise operation two different folders in python
I'm trying to combine all images one by one from two different folders, for example, I have those two folders
-folder1
-+img1.jpg
-+img2.jpg
-+img3.jpg
...
-folder2
-+img_1.jpg
-+img_2.jpg
-+img_3.jpg
...
0
votes
0
answers
55
views
Bitwise AND between hexadecimal arrays Numpy
I would like to perform a Bitwise AND operation between two hexadecimal numpy arrays. However, NumPy does not seem to allow this operation between these arrays.
a = np.array(['0x2a75210031157', '...
0
votes
0
answers
127
views
Bitwise AND Operation between multiple rows Pandas Dataframe
I would like to perform AND operations between multiple rows of a Pandas Dataframe by only reading the indices of the rows. For example:
def f(df, indices):
result = df.iloc[indices[0]]
...
0
votes
1
answer
67
views
AND bitwise operation over addition
I've been looking for a while now about this issue, yet I couldn't find any result.
assuming A, B and C are integers, is there a function (arithmetic or boolean) F and G such that:
(A + C)&(B + C) ...
0
votes
0
answers
15
views
Numpy Boolean Bitwise unexpected behavior
I have a code which takes a numpy array of prices, and find discounts in desired range.
For some reason, the output is not as expected.
My code is:
# prices
prices = np.arange(5, (1-0.4-0.01)*5, -0.1)
...
0
votes
1
answer
50
views
Bitwise in SQL Server
I need some help please, my view return the parent and child ID.
When the parentID is type N, M or SU, so the the view return the ID him self
Now, my issue is, when the child is C type, I would like ...
0
votes
1
answer
73
views
bitwise operation in sql
please can someone help me to understand this condition val1 & 11 = 0
SELECT val FROM table WHERE val1 & 11 = 0
available for more information.
Thanks a lot
0
votes
1
answer
173
views
How to calculate bit mask and bit shift values based on the following 3 fixed values?
I am supposed to determine the bit mask and shift values based on the following 3 values: the start byte, the start bit, and the number of bits.
I will receive some byte array for my data of x many ...
0
votes
0
answers
20
views
Bitwise on bytes
I'm trying to use the AND operation for two byte but it is reading as string.
mybytes1 = packet[0] & b'\x07'
Is there a way I can perform an AND operation for it to read as bytes?
TypeError: ...
-1
votes
1
answer
45
views
How does `(sum & operand) === operand` work in the following code? (Bitwise And) [duplicate]
The following code checks if sum has operand, the code is very straightforward, but I'm confused by the condition (sum & operand) === operand.
I've read the Bitwise AND (&) doc, confused me ...
0
votes
1
answer
51
views
Please run this code and tell me why got this result
why the result of " (num >> i)& 1 " is 1 for the second loop?
main() {
char num = 5 ;
int i , count = 0 ;
for(i=0;i<8;i++){
if ((num >> i)& 1 )
...
1
vote
5
answers
169
views
Which is better, double negation or bitshift?
I need a boolean-type function which determines if a bit, in a variable's bit representation, is set or not.
So if the fourth bit of foo is what I want to inspect, I could make the function return
!!(...
8
votes
6
answers
237
views
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 ...
0
votes
1
answer
83
views
Python bitwise & giving incorrect result
Following is my code, the output of expression d is incorrect. It is returning True where as it should have returned False, as all the components in the expression evaluates to False.
When I am ...
2
votes
1
answer
81
views
Java - &0xff gives bits from 8 to 31 as 1's
I have a problem while doing the AND operator using 0xff.
public Pixel(int x, int y, int p) {
this.x = x;
this.y = y;
pixelValue = p;
A = (byte) ((p>>24) & 0xff);
R = (...
1
vote
3
answers
143
views
The opposite of bitwise AND
I don't like so many nested ifs, which is why I want the opposite of that condition if (section_header[i].Characteristics & IMAGE_SCN_MEM_WRITE). What would it be? I tried with if (section_header[...
1
vote
0
answers
186
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
2
answers
327
views
Maximum XOR of Two Numbers in an Array and Bitmasking
LC: https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/
public int findMaximumXOR(int[] nums) {
int max = 0, mask = 0;
for (int i = 31; i >= 0; i--){
...
-1
votes
2
answers
81
views
do bitwise and operation with a given value which results in a desired output [closed]
I want to determine the value of X in the below mentioned operations:
X & 0x08 = 8
How to find out x in this scenario?
1
vote
1
answer
119
views
in Random.java, isn't x&((1L<<48)-1)==x? [closed]
See int next(int bits) from http://developer.classpath.org/doc/java/util/Random-source.html. It contains the line
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
The number (1L<&...
1
vote
2
answers
133
views
Is there a way to reverse this python function?
def function(value):
bit = value << 1
movebit = bit & 255
if (value> 127 ):
movebit = movebit | 1
return (movebit)
I have got this piece of code that I am ...
0
votes
0
answers
48
views
difference of array element equal to difference of bitwise or and bitwise and
Given a sequence A of n integers: we have to find the number of pairs (i,j) where
(A[i] | A[j]) - (A[i] & A[j]) = (A[i] - A[j])
For example suppose A is {4,4,2,1} so total 6 pairs exist
(1,1)
(2,...
-1
votes
2
answers
198
views
How to find the most significant bit of a signed integer in C
I need to find the most significant bit of signed int N and save it in signBitN. I want to do this using bitwise only operations.
Also, how would I make signBitN extend so that all its bits are equal ...
0
votes
1
answer
29
views
Why by using n&(n-1) is not able to calculate number of 1's bits?
I found a video on youtube that says by using n&(n-1) we can able to count number of 1's bits and it's much faster than - right shifting the bits by one and then checking the last bit . When I ...
0
votes
2
answers
115
views
PHP Bitwise compare values from database with an array
Actually i would like to compare some values in a table with an array and output the key with has equal value of the array.
Problem is: the values in the database are bitwise if i am right. Not sure ...