bit-shift

Catching overflow of left shift of constant 1 using compiler warning?

We're writing code inside the Linux kernel so, try as I might, I wasn't able to get PC-Lint/Flexelint working on Linux kernel code. Just too many built-in symbols etc. But that's a side issue. We have any number of compilers, starting with gcc, but others also. Their warnings options have been getting stronger over time, to where they ...

What is the best way to combine two uints into a ulong in c#

What is the best way to combine two uints into a ulong in c#, setting the high/low uints. I know bitshifting can do it, but I don't know the syntax, or there maybe other APIs to help like BitConverter, but I don't see a method that does what I want. ...

In C++, what is the difference between 1 and 1i64?

I'm converting some 32-bit compatible code into 64-bit - and I've hit a snag. I'm compiling a VS2008 x64 project, and I receive this warning: warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) Here's the original line of code: if ((j & (1 << k)) != 0) { And here's what it look...

Bit shifting N bits

Hello quick question regarding bit shifting I have a value in HEX = new byte[] { 0x56, 0xAF }; which is 0101 0110 1010 1111 i want to the first n bits, example 12 then shift off the remaining 4 (16-12) to get 0000 0101 0110 1010 (1386 dec) i cant wrap my head around it and make it scalable for n bits. Thanks! ...

Problems with bit-shifting in complicated expressions

I've distilled an equation down to this: speed = ( ( rear_wheel_speed_a + front_wheel_speed_a ) << 10 ) + ( ( rear_wheel_speed_b + front_wheel_speed_b ) << 2 ); but for some reason I'm getting unexpected results so I must be doing something wrong. This started out like this: speed = ((((rear_wheel_speed_a * 256 + rear_wheel_s...

Unexpected bitshift results

I'm initializing an unsigned short int with a = 0xff (all bits are set). Then I assign b to a>>7 which should yield (0000 0001) and it does. However, the odd thing is that when I assign c to a<<7, it isn't equivalent to (1000 0000). I tested this by outputting 0x80 (which is 1000 0000) and c, but they aren't the same. Here is some code:...

How to bitwise shift in VB.NET?

How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method? ...

Shifting the sign bit in .NET

I'm reading bits from a monochrome bitmap. I'm storing every 16 bits in a short in the reverse order. If the bit in the bitmap is black, store a 1. If white, store a 0. E.g.: for bitmap: bbbw bbbw bbbw wwww my short is: 0000 0111 0111 0111 The 1st way I tried to do this was: short m; // ... Color c = bmp.GetPixel(j, i); if (c.R == Col...

One position right barrel shift using ALU Operators?

I was wondering if there was an efficient way to perform a shift right on an 8 bit binary value using only ALU Operators (NOT, OR, AND, XOR, ADD, SUB) Example: input: 00110101 output: 10011010 I have been able to implement a shift left by just adding the 8 bit binary value with itself since a shift left is equivalent to multiplying...

PHP right shifting and negative result?

Hi, I've run into a problem whilst converting some C code to PHP, specifically in the use of the right-shift operator. edit: in the following examples, bit = 0; Original C code: p->param->outBits[bytePtr++] |= codeword >> (9 + bit); PHP code: $outBits[$bytePtr++] |= $codeword >> (9 + $bit); If I start with codeword being 130728,...

Dividing by arbitrary numbers using shifting operators

How can you divide a number n for example by 24 using shifting operators and additions? (n % 24 == 0) ...

What is the JavaScript >>> operator and how do you use it?

I was looking at code from Mozilla that add a filter method to Array and it had a line of code that confused me. var len = this.length >>> 0; I have never seen >>> used in JavaScript before. What is it and what does it do? ...

Right shifting negative numbers in C

I have a C code in which I do the following int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try the following printf ("%d %d", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Q-1: Is this expected? I am able to think something like 65535 >> 1 = (int)...

Why am I getting strange results bit-shifting by a negative value?

This question is NOT a duplicate of this question. I came across a situation where I might have had to left-shift a (positive) number by a negative value, i.e., 8 << -1. In that case, I would expect the result to be 4, but I'd never done this before. So I made up a little test program to verify my hypothesis: for (int i = -8; i <= 4; i...

Side-effects of x86 assembly right-shift operator SHR?

I'm tracing through a program with an ASM debugger ollydbg and I come across this code snippet, which is a loop segment: CPU Disasm Address Hex dump Command Comments 007D05EC |. 33C9 XOR ECX,ECX 007D05EE |. 8BFF MOV EDI,EDI 007D05F0 |> 8B54B4 10 /MOV EDX,DWORD PTR S...

When to use Shift operators << >> in C# ?

I was studying shift operators in C#, trying to find out when to use them in my code. I found an answer but for Java, you could: a) Make faster integer multiplication and division operations: *4839534 * 4* can be done like this: 4839534 << 2 or 543894 / 2 can be done like this: 543894 >> 1 Shift operations much more fast...

Using Maxima, I want to solve equations using bit-shifts operations

First, I've tried the stuff you get using "load(functs)", no thank you. I have some pretty complex equations that I'd like to evaluate in Maxima. But at one step in the process I split a register in it's most-significant and least-significant components, and I need to be able to do that in Maxima -- if I am to evaluate the class of te...

Java bitshift strangeness

Java has 2 bitshift operators for right shifts: >> shifts right, and is dependant on the sign bit for the sign of the result >>> shifts right and shifts a zero into leftmost bits http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html This seems fairly simple, so can anyone explain to me why this code, when given a value ...

Bitwise shift operators. Signed and unsigned.

I'm practising for the SCJP exam using cram notes from the internet. According to my notes the ">>" operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator "<<" is supposed to preserve the sign bit. Playing around however, I'm able to shift the sign with the "<<" o...

PHP Left Shift giving two answers on two different machines

Hello, I'm very confused about behaviour of PHP's left shift function. I'm using it on two different machines (dev and hosting), and they are giving me different answers. I've tracked it down to this calculation: (-3941404251) << 5; On one machine I'm getting the answer -1570884448; on the other, I get 0. On both systems, PHP_INT_MAX ...