8,023 questions
4
votes
3
answers
179
views
How to do a simple swap of two half of a 32bit integer in c?
I am trying to figure out a code that would be able to swap the higher half and the lower half of the binary form of an integer number such as for example:
0000000 00110011 01001001 11111110 turn into ...
-1
votes
3
answers
97
views
Python Bit Shifting [closed]
Assume I have a 1 source variable (int) which is 10 bits long. Let's call it src.
I have 2 destination variables (int) which are 8 bits long namely byte1 and byte2.
MSB LSB
src ...
2
votes
2
answers
223
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() ...
5
votes
1
answer
256
views
"Repacking" 64 to 40 bits in AVX2
I have an array of 64-bit words, which I need to compact by removing the most significant 24 bits of each 64-bit word. Essentially this code in pure C:
void repack1(uint8_t* out, uint64_t* in) {
...
-4
votes
2
answers
105
views
Is there a name for the high and low bits of a hex string?
In the hex byte A1 B2 C3 D4, ABCD would be the most significant bits of each pair, and 1234 would be the least significant. I find that these groups of bits are often operated on together in hardware-...
3
votes
1
answer
203
views
Why do bit manipulation intrinsics like _bextr_u64 often perform worse than simple shift and mask operations?
I'm working on performance-critical code and experimenting with different ways to extract bit fields from 64-bit integers. I expected that using intrinsics like _bextr_u64 (Bit Field Extract) would be ...
-3
votes
2
answers
127
views
Bit Manipulation [closed]
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void) {
int v;
int sign;
sign = -(v < 0);
printf("%d", sign);
}
Why ...
2
votes
1
answer
254
views
Difficulty in understanding the logic behind a problem based on bitwise computations
As part of a test I was given, I am required to code the instructions below:
A function F(x) is defined over integers x >= 0 as:
F(x) = ∑ [(x | i) - (x & i)] (summation ∑ is over i),
with i ...
-1
votes
1
answer
179
views
Counting the number of set bits
this is the problem(counting bits)from cses i have attached the link here https://cses.fi/problemset/task/1146/
the goal of the problem is to calculate the number of set bits in all the number from 1 ...
0
votes
1
answer
64
views
bit manipulation for big data processing with complex boolean logic
Looking into solving a very large data set problem by using bit manipulation to reduce compute
Imagine billions of records that look like this(but more than 20k in length):
Questions : A,B,C,D
...
1
vote
0
answers
63
views
How does xz's BCJ filter for ARM64 work with the ADRP instruction?
I was trying to understand the BCJ ARM64 filter in xz here,
(also the 7zip version for reference)
I don't understand the case for the ADRP instruction, this is the snippet for it from xz:
else if ((...
9
votes
3
answers
334
views
3D Morton code computation utilizing carry-less multiplication
This question arose specifically in the context of exploratory work for RISC-V platforms which may optionally support a carry-less multiplication instruction CLMUL. It would equally apply to other ...
1
vote
2
answers
110
views
Why does 32-bit bitmasking work in Python for LeetCode "Single Number II" when Python ints are arbitrary precision? [closed]
I'm trying to understand why the following solution for LeetCode's Single Number II works in Python:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
number = 0
...
0
votes
1
answer
168
views
Using SQL Server, looking to get subnet from IP address and subnet mask stored in the database
We have a use of subnets for doing geo location of computer assets. In the most up to date data we do not have subnets IE 10.81.66.0, we just have the IP address and subnet mask such as 255.255.255.0
...
0
votes
0
answers
68
views
How can one compand (or compress, or pack) four uint8_t values into one uint32_t? [duplicate]
I am sorry, I love C++, and so it is embarrassing that I have to ask: how can I store four uint8_t values in a single uint32_t? Or a uint64_t if necessary. This involves bit-meddling but I lack the ...
0
votes
2
answers
151
views
2's complement operator on C
I'm reading The C Programming Language by K & R 2nd edition and got to the bitwise operators. Why, on C, the Complement (~) Operator applied to a N number, returns -(N + 1) ? When it normally just ...
2
votes
1
answer
172
views
Optimization and Methods for Reversing Nibbles of a Byte
I'm currently studying encryption and I was coming up with ways of reversing the nibbles of a byte (ex. 0xF5 => 0x5F). I came up with this solution:
byte >> 4 | (byte & 0x0F) << 4
...
3
votes
3
answers
199
views
How do you obtain the upper 15 bits of a uint16_t?
I have the following 2 bytes to decode: 0000 0000 0111 1011
and I wish to fill this structure
struct ctn
{
uint16_t fx : 1;
uint16_t stn: 15;
ctn() : fx(0), stn(0) {}
} PACKED;
I store ...
3
votes
1
answer
125
views
In C language, assuming expression is equal to 0, why !(expression) is also 0?
My gcc version is 12.4.0.
code is below:
int x1 = 0x80000000;
printf("%d\n\n", x1 ^ (~x1 + 1));
printf("%d\n\n", !(x1 ^ (~x1 + 1)));
I though the output should be 0 and 1, since !...
0
votes
1
answer
75
views
Understanding this bit mask method
I saw an implementation of a bit mask in the following code, but I don't understand how it works. Can someone explain how the expression in the bit mask works? The author's comments are at the top of ...
1
vote
1
answer
117
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
133
views
C++ bit compaction
I'm working on a 16 byte array or 128 bits fully populated with the following bit fields:
struct data {
uint ttg : 16; // time to go, units:mins
int volts : 16; // units:10mV
uint ...
0
votes
0
answers
49
views
DCoder bitwise solution. As I can't see the test cases, it's difficult to understand what I did wrong
I'm using a website called dcoder.tech to help me test how well I use swift. They show you one test case and the results that your code should output. You write a program that takes input, the website ...
-1
votes
1
answer
131
views
Wrong output from custom big unsigned integer type when checking whether subtraction follow modular arithmetic
Here I am tasked with creating an UnsignedBigInteger class that can handle numbers bigger than a long. Am allowed to use a byte array as the internal representation, uint8_t[].
What I came up with is ...
2
votes
1
answer
188
views
Unpredictable value when converting byte array to int | Eclipse Temurin-17.0.10+11
Problem statement:
I am encountering an issue when running the following multi-threaded program. The program spawns a large number of threads (10,000) that process the same byte array value. The issue ...