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.

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right, often written as << and >> respectively.

For example 4<<3 = 32 and 5>>1 = 2.

A zero shift returns the integer part, e.g. 3.14<<0 = 3.

2051 questions
0
votes
1 answer

How I do circular shifting (rotation) of int in Python?

In Java right rotation is done using: (bits >>> k) | (bits << (Integer.SIZE - k)) But how to do similar thing in Python? I tried to do (as described here): n = 13 d = 2 INT_BITS = 4 print(bin(n)) print(bin((n >> d)|(n << (INT_BITS - d)) &…
adnanmuttaleb
  • 3,388
  • 1
  • 29
  • 46
0
votes
1 answer

Make a formula which will convert an index of array into ement(byte) at this index

need to make an algorithm (formula, function) using AND OR XOR NEG SHIFT NOT etc. which calculates the element of array from an index, the size of the element is one byte e.g. element = index^constant where the constant is array[index]^index…
0
votes
0 answers

JavaScript Number and bitwise operations

I know that JS Number types are IEEE754 double precision floating points. Given this fact, how does JS perform bitwise operations? -1 >>> 1 => 2147483647 Is it merely simulating bitwise operations programmatically or does the language actually have…
funct7
  • 3,407
  • 2
  • 27
  • 33
0
votes
2 answers

Extracting values frm a number in C

I have the following assignment: Considering that the 32 bits on an unsigned integer represent a date like this: Bits 0 to 7 are the day Bits 8 to 23 are the year Bits 24 to 31 are the month Implement the function unsigned int …
0
votes
1 answer

Can anyone explain me this line? c |= 1 << i;

I recently started C and for some reasons I couldn't get this line c |= 1 << i; the purpose of this function I found online is to get the least significant bit from an array and then combine it, and return as a byte. unsigned char getlsbs(unsigned…
Siv
  • 43
  • 6
0
votes
1 answer

How to perform division on two 32Q16 fixed-point integers?

I am trying to divide two 32Q16 numbers using fixed-point processing arithmetic. What I understand is that when we divide one 32Q16 fixed-point operand by another, we require the result to be a 32Q16 number. We, therefore, need a 64Q32 dividend,…
Rohan Saha
  • 15
  • 4
0
votes
3 answers

Flip the k-th significant bit

I would like to find a better way to achieve the following: n = 6 k = 1 Flip the 1st significant bit in 6 Variable Binary representation Decimal Representation n 110 6 m(result) …
Bence Szabari
  • 143
  • 1
  • 2
  • 12
0
votes
0 answers

How to emulate *really simple* variable bit shifts with SSE?

I have two variable bit-shifting code fragments that I want to SSE-vectorize by some means: 1) a = 1 << b (where b = 0..7 exactly), i.e. 0/1/2/3/4/5/6/7 -> 1/2/4/8/16/32/64/128/256 2) a = 1 << (8 * b) (where b = 0..7 exactly), i.e. 0/1/2/3/4/5/6/7…
nickpelling
  • 119
  • 9
0
votes
1 answer

Figuring out what exactly this bitwise AND and LSL does in C

1. long int temp; 2. int product = -1; 3. temp = (long int)product & 0xFFFFFFFF; 4. temp = temp << 32; I know that temp is 64-bit while product is 32-bit. I am slightly confused about lines 3 and 4. We are casting product as a long int, which…
qbuffer
  • 383
  • 4
  • 14
0
votes
1 answer

bitwise operation set nth bit for negative number

I am trying to set the nth bit of a number by using bitwise operation. I get the nth shifted 1 (n being the index of LSB starting from 0) and then use the OR operation to get set the nth bit. Works fine for positive numbers, and theoretically should…
0
votes
2 answers

Putting 2 integer values to make a long

I took over a project that has a spec partly implemented. I have a message with various fields. One of the fields has an ID (called DestinationID) of type long. Within this ID, it says the first 4 bytes(let's call it left) represent something one…
sparrow
  • 450
  • 5
  • 13
0
votes
1 answer

Python >> Operator returns different output than in javascript >>>

a = -506298134 d = 6 d = a >> d & 0xFFFFFFFF d = twos_comp(d, 32) def twos_comp(val, bits): """compute the 2's complement of int value val""" if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255 val = val -…
Biplov
  • 1,136
  • 1
  • 20
  • 44
0
votes
1 answer

How shifting operator works in finding number of different bit in two integer?

i was trying to find out number of different bit in two number. i find a solution here but couldn't understand how it works.it right shifting with i and and doing and with 1. actually what is happening behind it? and why do loop through 32? void…
Rakibul Islam
  • 325
  • 1
  • 3
  • 13
0
votes
0 answers

Using bit shifting to perform multiplication

I am trying to use Mips Mars to create assembly code that uses bit shifting and a loop to multiply any number up to 32 bits. But it is only iterating twice if that. I have already gotten the multiplier and multiplicand earlier in the program. $t0 =…
0
votes
2 answers

What does "ans += n << count" mean?

// Function for multiplication int multiply(int n, int m) { int ans = 0, count = 0; while (m) { if (m % 2 == 1) ans += n << count; // increment of place value (count) count++;…
Joshua Leung
  • 2,219
  • 7
  • 29
  • 52