3,108 questions
2
votes
3
answers
189
views
Bitwise operations act outside of type width in C; Compiler Bug or Within Spec?
The following derives from this question but is distinct
Consider the following code sample;
uint32_t *value = malloc(sizeof(uint32_t));
*value = 0xAAAABBBB;
int16_t *subset = (int16_t*) (value);
...
3
votes
2
answers
229
views
Why we use PIN & (1<<PBx) to check if PIN is high?
I'm a beginner working on AVR programming on the ATmega328P. I am trying to get input from a button and then control an LED.
void setup() {
DDRB |= (1<<PB5);
DDRB &= ~(1<<PB4);
...
2
votes
2
answers
227
views
Bitwise division in C : Programme seems to work for other numbers but when the divisor is 0 it returns 11 for no apparent reason
I am trying to solve the problem posed in this question which asks that division of two numbers be performed in a bitwise manner. So, I wrote the following functions.
sum() adds two numbers.
larger() ...
1
vote
1
answer
97
views
Bitwise operator or converts uints to int in golang
In a golang project I have defined those uints. They are set to uint automatically by iota:
package userrole
const (
Reviewee = 1 << iota // 1
Reviewer // 2
Admin ...
5
votes
3
answers
292
views
Finding the bigger number using bitwise operation in C
I am trying to write a computer programme that will take two numbers from the user as inputs and will print the bigger number using bitwise operation. I want it to end the programme and return 1 if ...
3
votes
2
answers
194
views
Programme works but says "warning: integer constant is so large that it is unsigned", solution?
I am trying solve the problem posed in this question that asks << 1 operation be performed on a 64 bit number using NAND operation only and without using any arithmetic operation. My attempted ...
0
votes
0
answers
29
views
Detecting overflow in two’s complement multiplication by 2 – which bits need to be checked
Given a number with a width of n bits representing a signed number in two's complement, what is the minimal number of bits that need to be checked and which ones, in order to know whether there will ...
0
votes
0
answers
85
views
Which of these two bitwise expressions is faster, and is there an optimized alternative?
I have two bitwise expressions and need to figure out which one is faster. Also, if there are other ways to implement the same, but in a faster way than the given expression. I am using these ...
4
votes
2
answers
273
views
Shift "<<" and bitwise "&" operators precedence issue. Why it doesn't compile?
This code doesn't compile.
It seems like the compiler (VS) is interpreting the expression as:
(std::cout << ul1) & (ul2 << std::endl)
#include <iostream>
int main() {
...
0
votes
0
answers
87
views
How to BIT inverse a value in OpenOffice Calc?
I need to bit inverse a value in a cell. All I found was the function XOR(a;b) and it would be possible to use a bitwise XOR to invert a value. However, the problem is that that XOR(a;b) only does a ...
-1
votes
1
answer
61
views
Assembly: Character type checking
So I'm practicing disassembling on an C program that checks if the user given serial number fulfills the requirements.
The program manually checks each character from the serial number, executing the ...
1
vote
1
answer
122
views
right shift not working correctly for large longs
when shifting bytes, there are some scenarios that do not seem to make sense, for example
printf("shifted bytes %llx\n",(((long long)1 << 63 ))>>1);
outputs c000000000000000, ...
-2
votes
1
answer
37
views
Salesforce Bitwise Comparison
I have a field containing an int that represents flags
0 = No error
1 = Error 1
2 = Error 2
4 = Error 3
8 = Error 4
so if I have a value of 5, I know it is Error 1 and Error 3
I need to add custom ...
-1
votes
2
answers
146
views
Unexpected output from right rotate function
I was trying to solve the exercise that says
Exercise 2-7. Write the function rightrot(b, n) which rotates the integer b to the right by n bit positions.
and i did code it, here is the code
#include ...
-2
votes
1
answer
98
views
Using bitwise operations to simulate an N-length hash table: is space complexity O(1) or O(n)? [closed]
I am solving a problem that requires O(1) space complexity to check for duplicate numbers. I wanted to use a hash table, but due to the O(1) space constraint, I used bitwise operations. However, I ...
3
votes
1
answer
148
views
Find all x such that (x & (x+y)) == 0
Given an unsigned 64-bit integer y, is there an efficient way to find all unsigned 64-bit integers x such that
(x & (x + y)) == 0?
Some examples for small y:
y=0: only possible solution x=0
y=1: ...
2
votes
1
answer
211
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
vote
0
answers
73
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
353
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
381
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
108
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
173
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
181
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
145
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
40
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" ...
-1
votes
0
answers
37
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
441
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
131
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
58
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
98
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
156
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
278
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
443
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
91
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
448
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
102
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
78
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
63
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
811
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
375
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
37
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
187
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
95
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
366
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
198
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
182
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
807
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
120
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
79
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 ...