2,103 questions
4
votes
1
answer
281
views
Implicit type casting within parenthesis and literals in C
The follow derives from the devkitpro 3ds filter audio example though it's not relevant to know exactly how the example works.
In short, consider this code:
size_t data_size = ...;
uint32_t data* = (...
1
vote
1
answer
62
views
Does the binary representation of a fixnum include a sign bit?
TL;DR:
In Emacs Lisp, is the sign of a fixnum part of its binary representation or stored in some metadata? What's the distinction between positive and negative fixnums?
Can the sign be switched in ...
1
vote
0
answers
124
views
Flag-setting shifts on AArch64? LSLS is not recognized. (Shift two registers together as a 128-bit integer)
I've been learning arm64 on a computer with a Mac M3 chip. The assembler (as) is not recognizing the lsls instruction which should be a logical shift left which sets the flags. (Also not lsrs.)
I am ...
3
votes
2
answers
193
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
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 ...
5
votes
2
answers
216
views
Inconsistent bitwise shifting result in C code
I'm writing a C program and need to create a bitwise mask that could potentially fill a computer word (64 or 32 bits) with all 1s. I found a bug in my code but I can't make sense of it because when I ...
2
votes
1
answer
257
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 ...
4
votes
2
answers
267
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() {
...
3
votes
1
answer
105
views
Avoiding undefined behavior when shifting in CUDA
In CUDA I regularly exploit the fact that the hardware does not limit the width of shifts.
This is unlike x86, where only the lower bits of the shift amount are taken into account.
Unfortunately I ...
0
votes
2
answers
243
views
How to rotate a 16-bit register in Z80
I have a value in a 16-bit register (say, HL). How do I rotate it left one or more times?
2
votes
1
answer
174
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
...
1
vote
1
answer
120
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, ...
3
votes
5
answers
158
views
MSP430FR5739 rotate bits in 16-bit unsigned integer
I am working on a project where I have to rotate bits in 16-bit unsigned integer using MSP430FR5739.
For example:
0111 0000 1101 1101 must become 1011 1000 0110 1110 after one shift to right.
I read ...
-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 ...
4
votes
1
answer
133
views
Arithmetic shift-right integers with half rounding toward zero
I am looking for an efficient algorithm that computes arithmetic shift-right of integers that rounds to nearest integer with halfway rounding toward zero behavior. An answer can be a proper ...
1
vote
3
answers
194
views
How to scale a 10 bit analog signal into an 8 bit PWM?
I'm using the PIC18F47-Q18 Curiosity HPC Development Board and the MPLAB X IDE v6.20.
The goal is to store the 10 bit value from the analog output into the 8 bit PWM. Depending on the justification of ...
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(...
0
votes
2
answers
133
views
How use BigInt that right shift operation and XOR in javascript?
I want move some of bit toward right in javascript, but I found result was not right because I wrote a test program by C language to contradistinction.
I spend whole day who had't found correct way. ...
0
votes
1
answer
77
views
How does bitshifting 0xFF to the end of an Uint32 form a complete hexadecimal code?
I've been following along to Cave of Programming's C++ for Complete Beginners on Udemy, and in lesson 67 he goes over bitshifting to output certain colour pixels in an SDL window. There's a certain ...
1
vote
2
answers
97
views
Shifting a number based on the remainder of it divided by 4 - C
I have a function implemented which reverses the bits in a number. The number of bits in the number to be reversed can vary. However, of course it must be shifted to the left at the end to align with ...
-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 ...
1
vote
2
answers
118
views
C++ reading unsigned int values from byte buffer
After more than 20 years off, I started to get back into coding again, and started re-learning C++ (a language I practically never used, I used to be an Object Pascal man). So, I consider myself a ...
1
vote
1
answer
227
views
Converting a float to a 16 bit binary value in C
This is my code. When I change the mantissa, exponent, and sign to 23, 8, and 1 respectively in order to represent a 32 bit number, my code works. However when the values are changed to represent the ...
0
votes
0
answers
80
views
C++ - Questions about bit shift operators (<< and >>) [duplicate]
Consider the below code:
std::string g_CharTable = "stjkloptrew23456mncv891TZUIOPAS";
std::uint32_t value = 123456789
std::string hash;
for (std::size_t i = 0; i < 5; ++i)
{
...
0
votes
1
answer
175
views
16 bit register converting to two 8 bytes using vb.net in visual studio
converting the 16 bit data to 8 bit and then reversing it back to 16 bits
Module Module1
Sub Main()
' Original 16-bit data
Dim originalValue As UShort = &HA91
' Split ...
5
votes
1
answer
432
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 ...
5
votes
2
answers
186
views
Confused about casting and order of operations
This is a very specific problem and I haven't heard back from the author, so I thought I would throw this out and see if my sanity is intact or not.
This is in regard to this video regarding DMA on a ...
0
votes
2
answers
98
views
Converting bit-shift operations to specific binary number format (for a PIC microcontroller)
I am working on several home automation projects with PIC12F675. To maximize portability, I am using bit-shift operations to map GPIOs, like this:
// Hardware Mapping
#define pressure_pin (1<<0)...
0
votes
1
answer
63
views
Any faster way to do this operation which loops for 16k times in c/c++
This code reduces a unsigned 32 bit integer into a 8bit as per our requirement.
Currently this operation takes almost 2.1 mili sec to complete. Can it be optimized to complete at least under 0.9 mili ...
-1
votes
1
answer
81
views
Left shift vs Mask Uint8Array
I wanted to know if anyone knows what would be faster and why.
do:
this.data[byte] |= 1 << bitIndex;
Or do1:
const BIT_MASKS: Uint8Array = new Uint8Array([1, 2, 4, 8, 16, 32, 64, 128]);
this....
0
votes
0
answers
46
views
MIPS not transferring values properly between registers (adding 28 for no reason)
I'm writing a program in assembly that simulates multiplication via iterative addition. I have the algorithm done, and for my first test case, I set my multiplicand to 50. Upon transferring this value,...
1
vote
1
answer
443
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: '*'
...
6
votes
1
answer
278
views
Unexpected Output from Bit Shifting using len on a string vs. string subslice
I have a Go program that performs bit shifting and division operations on the length of a string constant, but the output is not what I expect. Here's the code:
package main
import "fmt"
...
0
votes
0
answers
41
views
Why little endian? (Regarding shifts)
I'm a programmer, wondering about the impact of byte ordering on bit shift operations.
I've come back around to this topic time and time again but never found an answer to it:
Why does everyone hype ...
0
votes
1
answer
59
views
ATmega32: Bitshifting in the Code changes the Interrupt Overflow Duration
In our C Code we are Bitshifting a 1:
if(posizion_LED_MODUL == 3) //This statement is True, and this LED is Switched correctly
{
PORTC |= (1 << led);
}
This somehow messes ...
-2
votes
2
answers
77
views
Differences in the behavior of Bit shifting in C
I'm running a simple program to understand how bit shifting operates in C. The expression (temp << n) / 2^n is indeed equivalent to temp as I see. Reasoning this, it seems that the left shift ...
0
votes
0
answers
98
views
The fastest way to split uint64 into two others by digit position
I have a function like:
func splitNumberByDecimals(u uint256, dec uint) (intFraction, decFraction uint){
...
}
The task is to extract an unsigned number, and the number of decimal places for this ...
1
vote
1
answer
116
views
Is the bitwise `index>>=1` operation equal to the `index/2` and disregarding the remainder of the result?
So, I saw the following line index >>= 1 and I was wondering what did it mean. I did a little research and found out it was bitwise operations, but I had to simulate a few scenarios in my head ...
1
vote
0
answers
119
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
93
views
Explain how left shift works in Java
Why does the following program code output the value -32 to the console?
int num = 0xFFFFFFE;
for(int i=0; i < 4; i++) {
num = num << 1;
System.out.println(num);
}
The program produces ...
9
votes
3
answers
798
views
Purpose of integer literal suffix in left shift
In C, many operations employ bit shifting, in which an integer literal is often used. For example, consider the following code snippet:
#define test_bit(n, flag) (1UL << (n) & (flag))
As ...
1
vote
1
answer
136
views
Design Ones Counter Using Structural Level Modeling
Ones Counter Implement (Structural Level Modeling)
I am implementing a One's Counter that will count for the number of ones of an 8-bit binary value.
State Diagram
S0: start = 0 ↓
S1: R1 = ...
1
vote
1
answer
120
views
C++ floating-point addition with bit shift error issues
I am trying to calculate floating point addition with only using bit shifts and logical ands(&) and ors(|). I am aware that built in floating point addition is more practical, but this is needed ...
0
votes
1
answer
326
views
How to perform Arithmetic right shift on a 64 bit number stored across two 32 bit registers in ARM?
I am trying to implement the booth's multiplication algorithm in ARM assembly language.
Algorithm 2: Booth’s Algorithm to multiply two 32-bit numbers to produce a 64-bit result
Data: Multiplier in V , ...
3
votes
1
answer
179
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
2
answers
597
views
Divide by variable power of 2 using assembly
I've got this task:
dividePower2
Compute x/2n, for 0 ≤ n ≤ 30. Round toward zero.
Argument 1: x
Argument 2: n
Examples:
dividePower2(15,1) = 7
dividePower2(-33,4) = -2
This is what I've got so far ...
0
votes
2
answers
519
views
Why does the flags in ARM are not affected if there a change in the Register?
So I have this problem where
ANDS R1, R2, R3
and to assume the flags are initially
N=0 Z=0 C=0 V=1 Q=0
R1: 0000C5BF H
R2: 0000A0F9 H
R3: is 00000000 H
I have to examine the end results of the ...
0
votes
1
answer
472
views
JavaScript Error: Operator '<<' cannot be applied to types 'bigint' and 'number'
I am trying to bit-shift a bigint like so:
let foo = BigInt(420) << 32;
but I am getting the JavaScript Error: Operator '<<' cannot be applied to types 'bigint' and 'number'.
How do I bit-...
0
votes
1
answer
113
views
How do I create a sliding window of 5 bits in a single byte?
Here is a visual of what I want to have happen 5 bit sliding window
In words, I want a sliding window of 5 bits. I need to store a history of 5 events within a single byte. I'm using the 5 most ...
0
votes
2
answers
120
views
How to bit shift left or right based on sign of int?
I need to bit shift x by b, where b can be positive (shift left), zero (nop), or negative (shift right).
C bit shift doesn't handle negative shifts.
Can I define an inline func or macro to do this? ...