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.

Filter by
Sorted by
Tagged with
0 votes
2 answers
138 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 ...
Aidan M's user avatar
  • 47
-1 votes
1 answer
73 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-...
Cale McCollough's user avatar
0 votes
1 answer
60 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 ...
crose72's user avatar
0 votes
2 answers
85 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? ...
SRobertJames's user avatar
  • 8,581
0 votes
1 answer
84 views

Left shifting or Right shifting a integer with negative number, is it a defined behaviors?

I tried the below code and in both the cases i.e. (left shift and right shift) i got the output as 0 #include <bits/stdc++.h> using namespace std; int main() { int a=20; a=a<<-1;...
Guru Jadhav's user avatar
-1 votes
2 answers
91 views

Nibble_Swap or Swapping 0x1234

The Input is 0x1234 and I want the output as 0x2143 but I am getting 0x4321 #include <stdio.h> #define SWAP(num) (((num) & 0x0F) << 4) | (((num) & 0xF0) >> 4) int main() { ...
Saikumar's user avatar
0 votes
1 answer
61 views

What is the algorithm to count the number of 1 bits in a 64-bit integer? [duplicate]

For example: ./bit_count 0x123456789abcdef0 bit_count(0x123456789abcdef0) returned 32 What would be an algorithm to count the number of 1-bits in the 64-bit integer value that was passed in to the ...
Software Guy's user avatar
2 votes
2 answers
59 views

What are the 5bits and the 6bits in when bit shifting by the size of the integer?

I'm referring to this article specifically this part Oversized Shift Amounts: Shifting a uint32_t by 32 or more bits is undefined. My guess is that this originated because the underlying shift ...
Enlico's user avatar
  • 23.9k
0 votes
1 answer
44 views

Converting back from bitwise yield wrong expected value (Solved)

I'm trying to convert back from a byte buffer to it corresponding ulong, but can't recreate the value... I must be blind or doing something wrong here :( ulong us = (ulong)(millisecondsSinceUnixEpoch *...
Tiago Conceição's user avatar
-3 votes
1 answer
87 views

In real time project where we use shift operator and why?

I want explanation about in real time project where we use shift operator and why we use? in which type of project we use? detailed explanation from developers but not expecting default definition and ...
Kameshwar S SPK's user avatar
1 vote
1 answer
149 views

When does bit shifting cause undefined behavior in C++?

When does bit shifting cause undefined behavior in C++? And does the behavior differ between C++ versions and integer types?
palapapa's user avatar
  • 643
-1 votes
1 answer
61 views

c++ bit-shift right unexpected result [closed]

my code int p = 85; std::cout << std::hex << p << std::endl; std::cout << (p >> 1) << ' ' << (p >> 2) << std::endl; output: 0x55 // corrent! ...
user3600124's user avatar
0 votes
1 answer
91 views

Why does this Arduino bit shift work? Split byte into two 4 bit signed numbers

I'm trying to split a byte (8 bits) into two 4 bit signed numbers. The resulting two 4 bit numers can be stored in an 8 bit data type (i.e, int8_t or signed char), but I need the sign to be preserved. ...
ohobbs72's user avatar
2 votes
1 answer
60 views

Discrepancy in Bitwise Shift Calculation Results

I'm encountering a discrepancy in the output of my Go program, specifically with the variables x1 and x2. Here's the relevant code snippet: package main var n uint = 10 const N uint = 10 func main() ...
Bahamas's user avatar
  • 365
0 votes
1 answer
29 views

strange issue with PHP [closed]

I have been testing the bitwise operators in PHP and encountered a very strange issue. The following program <?php $foon = b'11111111'; for ($i = 0; $i <= 7 ; $i++) { $foo = (($foon &...
fat penguin's user avatar
1 vote
4 answers
174 views

Get an n-bit mask avoiding UB when n is equal to the bit width of the type?

I have a small positive integer n, and I use an unsigned integral type to store a mask containing n bits. Often, there is a need to construct a mask with all n bits set. For example, if n is 5, then ...
Bernard's user avatar
  • 5,299
1 vote
1 answer
130 views

What does bitshift do to a range in Rust?

I'm reading code that contains the following (slightly altered) Rust code: let new_var : Vec<...> = (0..1 << some_number) .into_par_iter() ...
NooneAtAll3's user avatar
1 vote
4 answers
315 views

How can I reliably perform an arithmetic shift right in C++?

The "arithmetic shift right" operation is similar to a normal (logical) shift right, except the most significant (i.e. shifted-in) bits are filled with the sign bit rather than 0. ...
Kevin H. Patterson's user avatar
0 votes
0 answers
101 views

java BigInteger - How does destructiveMulAdd convert a number to big endian magnitude?

I am doing this side project where I try to understand the internal implementation of some of the widely used utility classes in Java. I want to know how BigInteger store's the number internally. What ...
ng.newbie's user avatar
  • 2,910
-3 votes
1 answer
80 views

How does the function work, that writes a command to the arduino LCD1602?

I'm studying the Arduino and the LCD1602 display that comes with it without I2C. I have a sample code that displays information on this display, and I can't figure out how it does it. In the ...
Nizier's user avatar
  • 9
0 votes
1 answer
30 views

how do you use bit swift in python

import sympy as sp import sys import datetime import decimal start_time = datetime.datetime.now() x = sp.symbols('x') a = 1234123412341234 f_x = 1/x - a fp_x = sp.diff(f_x) # 미분 x_init = sys....
kimking's user avatar
3 votes
0 answers
35 views

Incosistent integer promotion when performing bitwise OR and leftshifting

I'm getting -Wconversion warning in C. conversion from 'int' to 'uint8_t' {aka 'unsigned char'} may change value [-Wconversion] When performing the following operation: uint8_t output_A = 0x00; ...
wzs's user avatar
  • 31
5 votes
4 answers
163 views

Where in the standard does it say that when you do bitwise operations, it will behave like big-endian?

Regardless of endianness, bitshifting and other operations always behave as if they are big-endian. So small numbers are on the right, and big ones are on the left. Also the bit for signedness is on ...
basedchad21's user avatar
1 vote
1 answer
186 views

c++ bitwise shift of long long int's in vector for example by 380 bits

i am searching for a efficient way to bitwise shift the whole vector of long long int's. The whole vector should be shifted not just each value separately. The shift value can be random for example ...
Miroslav Krajcir's user avatar
0 votes
1 answer
54 views

Why am I not able to add two integers with XOR, AND and left shift?

I tried running this on LeetCode problem Sum of Two Integers class Solution: def getSum(self, a: int, b: int) -> int: while b: withoutcarry = (a ^ b) b = (a &...
TheReal_Skywalker's user avatar
0 votes
1 answer
274 views

How do divide unsigned 128/64 Bit on x86-64?

I need to divide an unsigned 128-Bit number on a 64-Bit Processor at the Register rdx:rax with a 64-Bit divisor in rdi. The lower Bits are in raxand the higher ones in rdx. However the DIV-Instruction ...
HeapUnderStop's user avatar
0 votes
2 answers
205 views

Left Bit shift and casting

I have a behaviour that i don't understand, i try to construct an 64 integer from an array of bytes from big endian to little endian. uint64_t u; uint8_t bytes[2]; bytes[1] = 0xFF; u = bytes[1] <&...
mystackoverflowpseudo's user avatar
2 votes
3 answers
100 views

a |= (1 << 31) results in unexepected value

I made a reproducible code as below/ #include <stdio.h> int main(void) { long int a = 0x0; a |= (1 << 31); printf("a: 0x%lx\n", a); } I expect 'a' should be ...
Jason Park 's user avatar
0 votes
0 answers
48 views

C shift signed char gets [duplicate]

I accidentally trigger a bug in C code, my goal is to get rightmost 16 bits as unsigned from binary string. #include <stdio.h> #include <stdint.h> int main() { char msg[] = {0x01, ...
Dawn16's user avatar
  • 150
1 vote
0 answers
27 views

Little/Big Endian shifting position

I have written some code, that stores an integer from a char array[2304] Into an integer array,essentially, if my character array has the integer 1000, it is stored across 4 bytes memory_array[0]= ...
HyperCoderSuperion's user avatar
0 votes
0 answers
50 views

char and unsigned char different bit operations? || logic OR with char > 0xf0 not working as expected || 0x70 == '\x70' but 0x80 != '\x80' [duplicate]

I think my question boils down to: Why are char and unsigned Char treated differently even on bit operations? They have the same size and look the same found this but for me it does not explain why it ...
Alex's user avatar
  • 121
0 votes
2 answers
72 views

Storing hexadecimal bytes in Big Endian from Little Endian

I have this snippet of code here, which reads a file, using a program counter that begins at 0 It iterates by 4 bytes since each instruction is 4 bytes long (32 bits) Now my current code for (i = 0; ...
HyperCoderSuperion's user avatar
0 votes
1 answer
66 views

Bit shift operations on a long array in Java

How does one shift a long array n positions to the right or left e.g. do a >>> n or << n on this array long[] values = new long[]{ ...
linker's user avatar
  • 841
0 votes
1 answer
85 views

How VPSLLVD, VPSRAVD Instruction works?

I am studying MASM32, and I am learning about various shift instructions. I could understand standard shift instructions like SHL, SHR and so on. But I am not clear some complex shift instructions ...
HappyLuck99's user avatar
1 vote
0 answers
66 views

bitwise operation, change bit from 0 to 1 then 1 to 0 without affect the other bit in same variabel 8 bit (uint8_t)

I have a unsigned 8 bit variable SG1 which have value 0 or 1. I want to put SG1's value at a new 8bit variabel (eg:coil) at 4th bit without affect the other bit. can anyone help me ? example : uint8_t ...
Hisyam Faiz's user avatar
0 votes
5 answers
175 views

Does someone can explain me this line of C Code (Pointer Arithmetic, bit shift)?

Let *c be 32bit in Memory and xmc[] array of 32bit in memory (abstract: Network packet) xmc[0] = (*c >> 4) & 0x7; xmc[1] = (*c >> 1) & 0x7; xmc[2] = (*c++ & 0x1) << 2; ...
Builderhummel's user avatar
-1 votes
2 answers
64 views

Right bit shift in C keeps sign for variable int, loses sign for raw int, why?

I am currently trying to find a non-zero integer a such that a >> 31 == 1. The issue I'm running into is that any right shift of a negative number results in a negative number... unless I ...
frenzied NPC's user avatar
0 votes
3 answers
100 views

Question about using bitshift operators on int

I am a beginner so dont get mad at my simple question but suppose I have an int variable lets say a, and I do a<<3 will that be equal to a*2^3 = a*8 as I read that bitshift operators multiply ...
ani2016's user avatar
  • 15
0 votes
1 answer
51 views

How do I account for signed issues in binary bitshifts using TypeScript? (2^31 >> n)

I am making a checkers program that uses 32-bit binary ints to represent pieces on the board. To calculate possible moves, I apply bitshifts to the integers, but this doesn't work for the final bit. ...
Barnie Gill's user avatar
0 votes
3 answers
141 views

Bitwise operations (shifting return values)

if (((number >> i) & 1) == 1) this is my code example. How is the return value determined in this operation? We shift numbers to the right or left. what is the return value? if (((number >...
Eren Akbaş's user avatar
-1 votes
1 answer
41 views

What is the proper implementation of building a string representation of the contents of a byte array?

Appending the string builder with the following method is yielding incorrect results. The bytes in the byte array do not match the '1's and '0's represented within the resulting string. InputStream is ...
Harley Fioretti's user avatar
0 votes
2 answers
89 views

Same-line vs. multi-lined bitwise operation discrepancy

While writing a program for uni, I noticed that unsigned char byte_to_write_1 = (0xFF << 2) >> 2; ==> 0xFF (wrong) unsigned char byte_to_write_2 = (0xFF << 2); byte_to_write_2 = ...
Seth Rossman's user avatar
0 votes
0 answers
61 views

Its a program using a bitwise operators

The number of can types to be distinguished is N, and each can has L types of characteristics that can be answered with 1 or 0. Your objective is to distinguish between all N types of can by ...
Vicky's user avatar
  • 1
-1 votes
1 answer
47 views

Why after this method the result is different in Java and PHP and how to fix it?

With smaller numbers, this method works correctly in Java and Python, but with larger numbers, the result is different. What is the problem and how can I fix it? Java code: private static int combine(...
Snuiper228's user avatar
-1 votes
1 answer
90 views

Value of x when s = x >> 31 and x = (s & ~x) | (~s & x);

If x was to equal 12 in a 32 bit scenario, x = multiple 0's into the lsb 0000 1100. If the above scenario were to run, I believe I would get 0000 1100. Am I wrong? Along with that, what if I was to ...
Waldener's user avatar
0 votes
4 answers
133 views

How I can convert 1byte of data into 2bytes of data

I want to convert 1byte of array element into 2 bytes E.g arr[size] = {0x1F}; So, I want 0x1F will be stored in 2nd array like, arr_2[size] = {0x01, 0x0f} I have tried like following way... for(i=j=...
Bhushan Mahajan's user avatar
0 votes
0 answers
229 views

Gas savings in Solidity arguments -- structs vs encoded bytes

In my smart contract, I have a function that does something (not relevant), but there are two ways I could potentially accept arguments to the function: using a struct, or by encoding the values and ...
Marcus's user avatar
  • 3
1 vote
0 answers
54 views

Compressing 5 Bytes into 4 Bytes

I am trying to understand more about BCD, packed/unpacked byte arrays and byte shifting, in this picture I made an example of the instructions which I am unsure about, an array of 5 bytes is being ...
Se7en Axis's user avatar
0 votes
0 answers
59 views

How to bit shift in a bits where only a select bits are affected

I am really confused in a bit shifting thing, so the idea is I have 8 bits , the first 3 bits will stay constant no matter what, meanwhile all the wrapping around and bit shifting has to happen only ...
ani2016's user avatar
  • 15
-3 votes
1 answer
109 views

Can someone explain why these two things mean the same thing?

Here are two functions that do the same thing. Can someone explain how? void calculateFace(){ //READ THE DIP SWITCH int sw_one = digitalRead(LandingStartSW); int sw_two = digitalRead(...
Austin Pham's user avatar

1
2 3 4 5
42