Questions tagged [bit-shift]
A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right.
1,981
questions
0
votes
1
answer
44
views
Shift Left Operator Different Behavior [closed]
What is the difference between the following 2 pieces of code?
#include <iostream>
int main()
{
int i = 0x01 << 2;
std::cout << i << std::endl;
return 0;
}
and
#include ...
0
votes
2
answers
41
views
Swift Bitshift Integers in Array to the Right
I've found a similar example in Python.
Essentially, say you have an array which is [1, 2, 3], and in binary that would be [0b00000001, 0b00000010, 0b00000011], what's the fastest way to bitshift that ...
-4
votes
1
answer
29
views
How I can get integer part of number using JS bit shift operator?
So actually i know the solution of this problem, but i cant understand how its working.
The following function get whole part of decimal value. Who can explain me, how its works?
function ...
-1
votes
0
answers
50
views
Bitshifting causes arithmetic overflow warning in VS 2022
I am following a tutorial on how to make a raycaster in C++ with GLUT. Tutorial Link: https://www.youtube.com/watch?v=gYRrGTC7GtA while following this tutorial I use a bit-shifting technique for ...
-2
votes
1
answer
40
views
what is 0xf1 mean in the code, I am learning shift operators from GeekForGeek
what is 0xf1 mean in the code, I am learning shift operators from GeekForGeek
// Masking sign extension
class GFG {
public static void main (String[] args) {
char hex[]={
'0','1',...
10
votes
2
answers
1k
views
Is left shifting unsigned int more than its bit field width, but less than its type size undefined?
struct Type {
uint8_t var : 3;
};
int main()
{
struct Type bar;
bar.var = 1;
uint8_t baz = bar.var << 5;
}
According to the standard, left shifting more than the width of the ...
1
vote
2
answers
85
views
What is the >> operator really doing in this C++ code?
I am following along with Programming: Principles and Practice Using C++, and I am messing with the "get from" (>>) operator from some code in the third chapter. Here is a minimal ...
2
votes
1
answer
49
views
How to merge multiple numbers into one number like 4,0,0 into 400 without stdlib [closed]
I'm trying to parse a file that has following data eg:
MAGICNUMBER 400
4 is = 0x34
0 is = 0x30
4
0
0
are different unsigned chars
what i want is those different chars to be converted into
unsigned ...
2
votes
1
answer
61
views
Bitwise OR operation with 0b transform a given number in negative
I need to perform certain operations with bitwise operators, let's say we have this 40 bit unsigned integer:
1071698660929 when I apply to it a OR operator and an unsigned right shift operator I got ...
0
votes
2
answers
54
views
Does left shift operator wrap the bits around?
If my understanding of the left shift operator and little endianness is correct, then the value of x in the following should be 0 on little endian machines.
uint64_t x = ((uint64_t)1<<63);
(...
0
votes
3
answers
73
views
Why left-shifting operation with different result on __u32 and __u64?
I tried left-shifting a unsigned int by 24 like below,
__u8 buf;
__u32 u32_result = 0;
__u64 u64_result = 0;
buf=0xFF;
u32_result= (buf<<24); ...
0
votes
1
answer
28
views
How do you think masking bit problems?
For example in this answer to a reversing bits function problem made 4 years ago:
[reverse_Bits function]
https://stackoverflow.com/a/50596723/19574301
Code:
def reverse_Bits(n, no_of_bits):
...
2
votes
3
answers
82
views
Extract the first n bits of an unsigned int in C
I want to write a function which extracts the first n bits starting by the MSB of a given uint16. I'm working with bitmasks for the first time and haven't found an elegant solution as far.
I've ...
1
vote
4
answers
104
views
Do I have correct understanding of Undefined Behavior for shift operators in C?
I want to make sure I understand exactly under what circumstances the << and >> operators in C produce Undefined Behavior. This is my current understanding:
Let...:
x_t be the type of x ...
0
votes
4
answers
93
views
Modify higher bits of a variable in C
I have a variable in C with a binary value of '10010100'
and I have another variable with the value is '1111'.
What I want to achieve is to keep bits 7,6,1,0 intact and insert the second variable in [...
-1
votes
1
answer
42
views
explain binary_printf function [C lang]
Can someone please explain the binary_printf function below?
What are mask and shift and how does it work in line:
byte = (value & mask) / shift; // Isolate each byte.
I think it is useless it ...
0
votes
2
answers
102
views
Do I have to set most significant bits to zero if I shift right?
Let's say I have a 64-bit number and some bits that are set that hold a value, let's say three bits. I have a mask to get that value. To get the value of those three bits I bitwise 'AND' the number ...
0
votes
1
answer
49
views
Decoding Keyence LJ-X8000 Bitmap-Height Image
I have a Keyence Line Laser System LJ-X 8000, that I use to scan the surface of different objects.
The Controller saves the height information as a bitmap, with each pixel representing one height ...
0
votes
1
answer
53
views
Extraction of directory and page bits off a linear address
As many sources say that using ((1 << x) -1) << y isolates x bits
starting from y to y + x, and a linear address is built like this: 0:11 is the offset, 12:21 is the page and 22:31is the ...
0
votes
0
answers
56
views
Multiplying two integers with sizes 8N in C
I'm trying to multiply 2 integers each of size 8N, for even N. Both numbers are positive, and I have to do that by multiplying each time 8 bits on 16 bits.
This is what I have so far:
#include <...
-2
votes
1
answer
58
views
read and write to file in c - compressing matrix
I'm trying to write a compressed bits matrix to a and the read it back.
The writing works great but the reading keeps failing and I dont understand why.
typedef unsigned char BYTE;
int ...
0
votes
3
answers
107
views
Invert a value of bit in digit
I want to invert a value of bit in digit.
The method should invert value by number of bit, like this:
public static void main(String[] args) {
int res = flipBit(7,1);
}
public static int flipBit(...
-1
votes
2
answers
71
views
Partition negative and positive in an array in java using shift operator
You will be given an array of n integers, both negative and positive. You need to partition the array into positive and negative numbers. Add all the positive integers of the array to one array (or ...
1
vote
0
answers
28
views
Left Shift subroutine in LC3 not working for when the value in R3 is 0
I have to write a subroutine to calculate the value stored at R2 to left-shift by
the value stored at R3 (R2 << R3). The result is saved at R3. So far I have this:
leftSHIFT ADD R0,R2,R2
...
1
vote
1
answer
29
views
Python: How does one print bitshifted inputs as an integer?
I am trying to create a bitshifting tutorial script that takes a users input and prints the result, but it keeps returning the below error
DATASET NUMBER: 0
Traceback (most recent call last):
File &...
0
votes
1
answer
70
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 ...
0
votes
1
answer
69
views
Rotate and reflect a 5x5 bitboard
I am trying to find a fast way to rotate and reflect a 5x5 board to store it in a transposition table. The board is represented as a bitboard as they are very fast.
The bitboard is represented like ...
0
votes
1
answer
89
views
Map set of bitmasks to number of occurrences
Let's say I have n bitmasks (n is arbitrary, probably a number from 9-25). The length of these bitmasks (which is equal for all of them) does not matter, but for the sake of an example, let's say we ...
5
votes
4
answers
270
views
what is the most efficient way to flip all the bits from the least significant bit up to the most significant last 1 bit value?
Say for example I have a uint8_t that can be of any value, and I only want to flip all the bits from the least significant bit up to the most significant last 1 bit value? How would I do that in the ...
0
votes
0
answers
23
views
Bit shifting Left yields a negative number? [duplicate]
I'm trying to bitshift a number. Usually it works as expected, clipping anything above 32 bits.
num=2200131600391;
console.log((num).toString(2)); //100000000001000010000100000000000000000111
...
0
votes
2
answers
36
views
Bitwise operation and arrow function return statement
I have a byte, and I want to increment bits left to the first bit by 1 (the context is a small Conway's Game of Life).
Example: 11 is 0000 1011:
I want to increment 101
5 + 1 = 6 is 110
reset the ...
3
votes
0
answers
79
views
Right shifting unsigned variables gives strange result - C / GBDK
I'm developing a gameboy game in GBDK but I've got a problem with right-shifting (8bit) unsigned variables. The code looks like this.
#include <gb/gb.h>
#include <stdio.h>
#define ...
2
votes
1
answer
36
views
How does the BitConverter reconstruct bytes to a short
So I'm currently trying to learn more about endianness and how bytes are converted to shorts, ints etc.
And I figured I'd start simple, I'd start by converting a short ("30000") into two ...
-1
votes
1
answer
120
views
How to implement unsigned right shift for BigInt in JavaScript?
I tried this sort of implementation, but it doesn't appear to be working.
function urs32(n, amount) {
const mask = (1 << (32 - amount)) - 1
return (n >> amount) & mask
}
...
0
votes
1
answer
20
views
Shifting and negating sBytes producing different type promotions
I have my sbyte (41) which is 101001 in binary then I shift It twice to the left and It becomes -92 which is 1111111110100100 in binary meaning that It becomes a short.
But I know that whenever a ...
1
vote
1
answer
106
views
x86 assembly language rotate bits of characters to encrypt / obfuscate
I am trying to make an application that can encrypt and decrypt a string with assembly language using the ROR/ROL instructions to encrypt/decrypt.
I use the following code to encrypt using an array of ...
0
votes
0
answers
76
views
Bitshifting a uint64_t gives wrong result
As was said in the title, bitshifting a uint64_t type gives wrong results. I have absolutely no idea why this is the case. It seems like on the edges of the shifted bytes, it's modified the values to ...
0
votes
2
answers
74
views
Getting unexpected results when shifting a 32-bit integer more than 32 times
I would like clarification on why some bit shifting is doing something I wouldn't expect.
I developed a function, setBitsVersion1 that sets bits to 1 in an array of 32 bit integers using the regular ...
0
votes
1
answer
111
views
Set whole byte if first bit is 1
I'm trying to make program that will for for example 273(000100010001b) make to 4095(111111111111b).
Funny part is that my program works for first 2 iterations, for example 17(00010001b) correctly ...
-1
votes
1
answer
58
views
How to understand this C operators question?
I just started to learn C programming with books, and I found a part of the code shown below. When I run this code, the result was another value (2008000) instead of 0xffff.
int i = 0;
unsigned int ...
0
votes
0
answers
19
views
Convert strings to int using many operators
I had a string of int as a result of converting text to int. Here is its code in python:
def mix(data):
b = len(data)*8
m = bytes_to_long(data)
c = 0
for i in range(b//2):
if (...
0
votes
2
answers
51
views
How to use bitwise to convert a 4-bit binary number to an 8-bit binary number?
My goal is to write a bitwise expression to represent an 8-bit number in the right column by a 4-bit number in the left column.
Hope someone will help me to solve this question.
Thanks very much!
4 ...
1
vote
1
answer
75
views
Why is the Overflow-Flag only set when single shifts are used?
In the x86 intel reference manual it says:
"The overflow flag is set only if the single-shift forms of the instruction are used. [...]"
But when I have the following scenario:
xor eax, eax
...
4
votes
2
answers
145
views
Bitshift on structures
I'm unsure if this is possible due to structure padding and alignment but, assuming you take care of that by aligning your structures to 4/8 bytes, is it possible to bit shift on a structure as if it ...
3
votes
1
answer
76
views
Fast way of checking for alignment of in a 6x6 bitboard
I am trying to find a quick and fast way to check for alignment of 5 bits in a 6x6 board in all directions (diagonal, horizontal, vertical). The board is represented as a bitboard as they are very ...
0
votes
1
answer
32
views
How to fix bit length when doing bitshift right?
I have generated x = 0 - 2047 in hexadecimal (0 - 7ff).
Then, I tried to shift one bit to right, but I want to keep all the value.
for example, for x = 2047 = h7ff.
I want to shift this value to right....
-1
votes
2
answers
79
views
Why is bitwise OR operation not working as expected when an unsigned char is ORed with 11100000?
I am not able to understand why the operation 'c | 11100000' does not seem to work. But I also noticed that 'c | 10000000' works as expected.
#include <stdio.h>
int main()
{
unsigned char c,...
0
votes
0
answers
15
views
Given an integer how can I determine the highest alignment of its value?
My solution would have been to set ones as the last bits and & them with the value to test, and if it's zero then it's aligned for the number of bits set (times 2, or bitshifted 1). For example if ...
1
vote
1
answer
56
views
Arithmetic right shift on logic (2 dimensional) signal
I am trying to do a Shift Right, Arithmetic (keep sign) on the signal in.
When I set the value in[0] to 16'hbb00, I expect in_sign_extend[0] to be 16'hf760 after it is signed right shifted. But, I ...
-1
votes
1
answer
32
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 ...