3,099 questions
2
votes
1
answer
90
views
C invert the bits of a 16 bit integer
This code won't simply invert the bits of the 16-bit integer as expected:
#include <stdint.h>
typedef uint16_t U16;
U16 myNum = 0xffff;
~myNum // 0xffff0000, not 0x0000
Because C will promote ...
-1
votes
0
answers
51
views
Is this an efficient way to "rotate" the bits in a bitmask? [closed]
I'm doing some coding challenges for myself, and one of them is efficiently handling and processing the shapes and rotations of Tetris pieces. I decided that storing the initial piece configuration ...
1
vote
0
answers
58
views
question at / pivot and >> 2 in C++ Solution for Next Higher Number
I came across the following C++ solution for finding the smallest number greater than n that has the same number of 1's in its binary representation
int solution(int n) {
int pivot = n & -n;
...
0
votes
2
answers
123
views
Convert IEEE Float hex string to an Excel VBA Single (float)
I have an input string read from a device which is an ASCII Hex representation of a 32-bit floating point value.
e.g. "4048f5c3" (--> 3.14)
My question is how can I turn that into a ...
1
vote
5
answers
325
views
Can bitwise operations do this?
I have a basic logic that I can implement with logical operators:
if a == true && b == false
I am wondering if I can do the same with bitwise operators?
XOR does this:
0 ^ 0 = false
0 ^ 1 = ...
2
votes
1
answer
77
views
Checking register value is setting the bit using C function
I have a snippet of code below that checks whether a register value is set.
# define My_register_array(index) (*((P2VAR(uint32, AUTOMATIC, APPL_VAR))Group_register(index))) /*eg: Group_register(...
3
votes
1
answer
101
views
Can I perform a bit-wise group by and aggregation with Polars `or_`?
Let's say I have an auth field that use bit flags to indicate permissions (example bit-0 means add and bit-1 means delete).
How do I bitwise-OR them together?
import polars as pl
df_in = pl.DataFrame(...
-3
votes
1
answer
167
views
Why does the right shift operator give unexpected results for negative numbers? [duplicate]
I'm trying to understand how the right shift (>>) operator works in JavaScript, especially when dealing with negative numbers. I know that the left shift operator (<<) effectively ...
35
votes
3
answers
2k
views
Unexpected behaviour during implicit conversion in C
I was helping a student with his assignment and ran into a very gnarly "bug" which I could not explain, and was wondering if I could get help in understanding what exactly is going on.
The ...
0
votes
1
answer
129
views
A single byte that is from 0 to 255 to be rescaled from 0 to 7
A single 1 byte could be from 0 to 255. I intend to rescale it to be from 0 to 7. I have done this:
bit8 := uint8(136)
// To re-scale 1 byte from 0 to 2^3-1 i.e. 0 to 7
bit3 := bit8 / 0xff * 0b0111
...
0
votes
2
answers
29
views
Performance of BIT_AND vs relational table
I have a case where users may have access to some content with subscriptions, it's not video streaming but I will write an example with that for easier understanding:
users in "basic" ...
0
votes
0
answers
31
views
Representing an 8-digit hexadecimal as a long [duplicate]
I am writing a Java program that performs bitwise operations on bites in a data stream.
I have been given test values in the form of 8-digit hexadecimal strings, which I have converted to base 10 and ...
5
votes
1
answer
220
views
Logical shift right without dedicated shift instruction
I am working with an assembly language that does not contain either multiply, divide, or bit-shift instructions. I am aware that a left bit-shift can be achieved by just adding the same number to ...
-1
votes
3
answers
126
views
C segfault question: if one of two inputs is null, return the other
I have a function that takes two strings and returns another string (if you must know, its my implementation of strjoin).
Now, I want to make it so that if one of the input strings is NULL, it will ...
0
votes
1
answer
38
views
Dask: In-place Modification with Boolean Indexing Causing Unexpected Behavior
I would like to do an in-place bitwise operation on my dask array i, with a mask cover it.
MVE:
import dask.array as da
i = da.full((10,10),fill_value=4)
c = da.ones(i.shape, dtype=bool)
c[:,0] = ...
0
votes
1
answer
91
views
Randomizing the choice of ports (uint8_t bitmask) efficiently
Assuming I have 4 ports that may be on or off, and are represented by the LSBs of a uint8_t mask, meaning 0b00001001 means ports 0, 3 are on.
Given a number between 0-3 (which represents the ...
0
votes
1
answer
118
views
How can I perform bitwise operations in python exactly how javascript would?
I am trying to write the following javascript function in python
exports.hash = u => {
u += 0xe91aaa35;
u ^= u >>> 16;
u += u << 8;
u ^= u >>> 4;
let a =...
0
votes
2
answers
136
views
Understanding the use of bitwise-operator in ternary-operator
I'm just learning how to code and while studying sorting algorithms I was asked to "see if you can combine the two lines in your if and else blocks into one line to make your code look a little ...
0
votes
0
answers
176
views
Problem understanding a bitwise XOR operation code in rust
I have the following code, the purpose of it is to find 14 distinct letters in a string of letters. It does it by using a left shift and XOR operator, I don't fully understand why the left shift is ...
0
votes
3
answers
79
views
Is there a general approach for optimizing bitwise expressions that distinguish two arbitrary sets of integers?
For context, I need to write a test for an integer between 0 and 7 inclusive, that evaluates to true for {1,3,4,6} and false for {0,2,5,7}. I thought for a couple minutes about whether there might be ...
1
vote
1
answer
212
views
Clang-tidy-18 `hicpp-signed-bitwise` "use of signed integer" false positive?
This might turn to be a stupid question but I really don't understand why clang-tidy is complaining here.
Consider the following config:
# .clang-tidy
---
FormatStyle: file
WarningsAsErrors: '*'
...
1
vote
1
answer
80
views
Why does bit vector a = [01101001] encode set A = {0, 3, 5, 6}?
I'm reading Computer Systems: A Programmer's Perspective and I don't understand why does bit vector a = [01101001] encode set A = {0, 3, 5, 6}?
I did some googling but haven't find anything useful. I'...
0
votes
2
answers
54
views
convert left shifted LSB bits to 1
I need to convert LSB 0 to 1 in left shift:
x=5;
int num = 0x02;//0b00000010
shiftVal = num << x;// 0b01000000
shiftVal should convert to 0b01011111
-1
votes
1
answer
57
views
How to remove option from bit wise operation [closed]
I am working in a WPF app where I have some Items checked, I am trying to clear the checked items and reset it in the for loop if the is check is true. Using ' fruit.FruitType = NewFruitType.None;' ...
1
vote
5
answers
498
views
Negate every bit value of a binary in C
So I got a coding problem, the the program asks me to negate every bit of a binary, for example, the regular bitwise negation will be like this:
ten = 00001010 the negation is 11110101 = 245, well to ...
2
votes
1
answer
231
views
AVX512 perform AND of 512bits of 8-bit chars
I'd like to AND two vectors of 512 bits containing 8 bit elements.
Looking at the Intel Intrinsics Guide I can see some 512-bit AND operations:
__m512i _mm512_and_epi32 (__m512i a, __m512i b)
__m512i ...
0
votes
0
answers
24
views
Minimizing the number of basic arithmetic/binary operators needed to arrive at all others
Consider the following operations:
Bitwise AND
Bitwise OR
Bitwise NOT
Bitwise XOR
Another bitwise operator like NAND or NOR
Binary addition
Binary subtraction
Binary multiplication
Binary division
...
0
votes
0
answers
32
views
How to calculate left shift for big powers?
In order to perform left shift, we have to multiply the left operand with the 2^second operand.
For big powers How to perform left shift manually?
For small numbers such as 3<<1
It will work as ...
2
votes
4
answers
167
views
The difference between (x^y) and !(!(x^y))
I am a new computer science undergrad. For my assignment I need to implement isNotEqual(int x, int y) function in C by just using bitwise operators. This function will return false if x and y are ...
0
votes
1
answer
46
views
Difference between logical "and" and bit wise & [duplicate]
I was writing a piece of code for a practice problem where I was trying the display the highest odd number. I kept running into a problem where when I was linking two statements together with a & ...
1
vote
0
answers
245
views
Lua 5.1 bitwise operations using arithmetic for 64bit numbers
Lua 5.1 does not yet support bitwise operators. The Lua environment I'm using is restricted and provides a bit32 library that allows bitwise operations with 32-bit numbers.
The issue is that I'm ...
-1
votes
1
answer
160
views
Why do x&1==0 and !(x&1) not yield the same results in an if statement in C++? [duplicate]
I had both of these conditions inside an if statement to check if x was even or odd, but the !(x&1) seemed to execute the body of the if in case of an even x, while x&1==0 didn't execute it.
I ...
1
vote
1
answer
124
views
How many additions operation can be performed instead of single multiplication in FPGA?
How many addition operations can be performed instead of a single multiplication on FPGA? In terms of used resources - as an example - energy and logic area cost. I would like to know it for multiple ...
1
vote
2
answers
502
views
Bitwise Reduction Operators in C
Are there unary bitwise reduction operators in C like there are in Verilog?
Like in Verilog we have:
$display (" & 4'b1001 = %b", (& 4'b1001));
and the output of the function ...
1
vote
0
answers
99
views
How does python enable bitwise operators without an integer bit limit [duplicate]
Since python has no limit on the number of bits for storing an integer, how exactly does it perform bitwise operations on integers?
How many bits does it really store an integer in?
RealPython claims ...
1
vote
1
answer
70
views
Major Speedup Question for a for loop in Pandas/Numpy on a bitwise_xor accumulate
Ok i am using a for loop as show below to convert this data from to the one below
using xor accumulate. For the entries i have (830401) rows and this is very very slow. is there
any way to speed up ...
-1
votes
1
answer
54
views
Why does Python return 0 for this & operation?
I'm learning Python and messing around with operations when I tried
>>> 14 & 16
0
>>> 0b1110 & 0b10000
0
In my head it would look like this:
>>> 0b1110 & ...
1
vote
2
answers
231
views
Sign extending bit shifted binary values in C++ using only bitwise operators
I have written the following function which is meant to extract bits 5 through 8 (inclusive) of an integer and return those bits. However, the extracted bits are treated as signed so therefore they ...
-2
votes
1
answer
141
views
I cant verify seed generation if someone can see what is wrong [closed]
So i got this function that generates seeds makeseed() and now im trying to make a "verify" seed keep in mind that any seed generated IS Correct so i want it so that the user can input seeds ...
1
vote
1
answer
100
views
how to detect size mismatch between variable and mask with bitwise operators
I want to check that a bitwise operator mask being used with a variable in a C source file is within the minimum range of the variable. I do get a warning if I cast the too large mask however I would ...
0
votes
1
answer
99
views
time issue with my C code on bitwise operator. Please tell me some changes so that my C code becomes efficient to run in minimum time
My code for this problem fails due to the time limit:
Given integers n and k as space-separated numerals in the standard input stream, print on separate lines the maximum value of { a AND b | 0 < ...
4
votes
1
answer
129
views
Is there a non-owning reference similar to std::bitset to provide bitwise operation and count for data in other container?
I am trying to implement a C++17 portable and efficient Hamming distance function for ORB features, hopefully automatically using SIMD when compiling for both x86 and ARM.
Problem with std::bitset
std:...
0
votes
2
answers
150
views
Can anyone simplify this bitwise expression?
Working on an ECS implementation in C++ and I have an itch that this expression can be simplified but I'm honestly not confident enough with bitwise operations to figure it out:
(x & y) == x
If ...
-1
votes
1
answer
51
views
What is the term for a function/operation that is reversible by using the same function again?
What do we call a function or operation that follows the same steps for transforming a value in either direction?
XOR is a simple example of this. Let's say my operation to prepare a byte for I/O is ...
3
votes
1
answer
142
views
Why does b >>> 1 always equals -1 when b is a byte and has value -1 in Java?
I know that >>> is unsigned shift-right, and since byte in Java is signed 8-bit then -1 will be represented as 1111 1111. As a result, for the code below:
byte b = (byte) -1;
b >>>= ...
0
votes
0
answers
69
views
Interpreter Pattern on Byte Array
I´ve got an byte array where variouse bits have a meanings. Now I´m wondering if I should use the interpreter pattern to define the array´s meaning and the pattern builds some dto´s out of the ...
1
vote
4
answers
401
views
Is this right way to set bits of enum?
I have bitwise enum for save a two state settings of some class. I try to develope easy function to switch the bit ON or OFF.
Is this proper and quick way to do it?
enum myEnum { eeNull = 0, ee1 = 1, ...
0
votes
1
answer
325
views
How to get the nth bit of a given decimal number? [duplicate]
I am trying to get the nth bit of a given number (starting from the end of the binary representation of the number (from 0)).
So for example let's say we have the decimal number 7 and want to get the ...
2
votes
2
answers
367
views
Writing a uint8 variable on two bytes?
So a bit of a weird one because I am working with an interface in which I do not have access. But the gist of it is this: I have an array of 16-bytes, 16 uint8 elements. The first 12 elements are ...
0
votes
1
answer
69
views
BigInteger logical operation takes more time in subsequent loops C#
I have the below console application to check the time taken for doing a specific operation with BigInteger in C#.
using System.Diagnostics;
using System.Numerics;
Stopwatch timer = new Stopwatch();
...