bit-shift

What's the reason high-level languages like C#/Java mask the bit shift count operand?

This is more of a language design rather than a programming question. The following is an excerpt from JLS 15.19 Shift Operators: If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. If the promoted type of the left-hand operand is long, ...

C# bit shift: is this behavior in the spec, a bug, or fortuitous?

I was working with bit shift operators (see my question Bit Array Equality) and a SO user pointed out a bug in my calculation of my shift operand--I was calculating a range of [1,32] instead of [0,31] for an int. (Hurrah for the SO community!) In fixing the problem, I was surprised to find the following behavior: -1 << 32 == -1 In fa...

Replace branch statements by bit-shifting operations

Hello, I'm writing an image binarization algorithm which simply converts each pixel's luminance value (grayscale image) to a black or white. Currently the algorithm for binarizing each pixel is roughly this if( grayscale[x] < thresholdValue) { bitonal[x] = 1; } (this is actually a simplification of the ACTUAL algorithm because the bit...

Why arithmetic shift halfs a number only in SOME incidents?

Hey, I'm self-learning about bitwise, and I saw somewhere in the internet that arithmetic shift (>>) by one halfs a number. I wanted to test it: 44 >> 1 returns 22, ok 22 >> 1 returns 11, ok 11 >> 1 returns 5, and not 5.5, why? Another Example: 255 >> 1 returns 127 127 >> 1 returns 63 and not 63.5, why? Thanks. ...

Bitshift in javascript

I've got a really big number: 5799218898. And want to shift it right to 13 bits. So, windows-calculator or python gives me: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 70791 | 10001010010000111 As expected. But Javascript: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 183624 | 10110011010...

How to parse bit fields from a byte array in Java?

I've been given the arduous task of parsing some incoming UDP packets from a source into an appropriate Java representation. The kicker is the data held within the packets are not byte aligned. In an effort to make the protocol as tight as possible, there are a number of bit fields indicating the presence or absence of data fields. Fo...

C++ shifting bits

I am new to shifting bits, but I am trying to debug the following snippet: if (!(strcmp(arr[i].GetValType(), "f64"))) { dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tmp_data >> 8; dem_content_buff[BytFldPos + 2] = tmp_data >> 16; dem_content_buff[BytFldPos + 3] = tmp_data >> 24; ...

How to produce 64 bit masks?

Based on the following simple program the bitwise left shit operator works only for 32 bits. Is it true? #include <iostream> #include <stdlib.h> using namespace std; int main(void) { long long currentTrafficTypeValueDec; int input; cout << "Enter input:" << endl; cin >> input; currentTr...

What does "<<" mean in C#?

Basically the questions in the title. I'm looking at the MVC 2 source code: [Flags] public enum HttpVerbs { Get = 1 << 0, Post = 1 << 1, Put = 1 << 2, Delete = 1 << 3, Head = 1 << 4 } and I'm just curious as to what "<<" does. ...

Ever any performance different between Java >> and >>> right shift operators?

Is there ever reason to think the >> (signed) and >>> (unsigned) right bit-shift operators in Java would perform differently? I can't detect any difference on my machine. This is purely an academic question; it's never going to be the bottleneck I'm sure. I know: it's best to write what you mean foremost; use >> for division by 2, for ...

PHP equivalent javascript >>> shift right with zero fill bitwise operators?

May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript. I just managed to discover a function as follow: function zeroFill($a, $b) { $z = hexdec(80000000); if ($z & $a) { $a = ($a>>1); $a &= (~$z); $a |= 0x40000000; ...

Negative logical shift

In Java, why does -32 >>> -1 = 1 ? It's not specific to just -32. It works for all negative numbers as long as they're not too big. I've found that x >>> -1 = 1 x >>> -2 = 3 x >>> -3 = 7 x >>> -4 = 15 given 0 > x > some large negative number Isn't >>> -1 the same as << 1? But -32 << 1 = -64. I've read up on two's complements, but still ...

Efficient bitshifting an array of int?

Hi, To be on the same page, let's assume sizeof(int)=4 and sizeof(long)=8. Given an array of integers, what would be an efficient method to logically bitshift the array to either the left or right? I am contemplating an auxiliary variable such as a long, that will compute the bitshift for the first pair of elements (index 0 and 1) and...

Efficient way to manipulate large powers of two

The most efficient way to code powers of two is by bit shifting of integers. 1 << n gives me 2^n However, if I have a number that is larger than the largest value allowed in an int or a long, what can I use to efficiently manipulate powers of 2? (I need to be able to perform addition, multiplication, division and modulus operations on...

C++ bit shifting

Hi, I am new to working with bits & bytes in C++ and I'm looking at some previously developed code and I need some help in understanding what is going on with the code. There is a byte array and populating it with some data and I noticed that the data was being '&' with a 0x0F (Please see code snipped below). I don't really understand ...

Bit shift and pointer oddities in C, looking for explanations

Hi all, I discovered something odd that I can't explain. If someone here can see what or why this is happening I'd like to know. What I'm doing is taking an unsigned short containing 12 bits aligned high like this: 1111 1111 1111 0000 I then want to shif the bits so that each byte in the short hold 7bits with the MSB as a pad. The resu...

circular shift c

I have to shift the int one place to the right and return it In Java i can just return n >> 1; Is this possible in C? The method we were given is as follows // Return n after a right circular 1-bit shift unsigned int right_circular_shift_1(unsigned int n) { ...

Getting value of LSB from Hex (C code)

Hi - first post here :) I've got a code like this in C: unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07}; I need to extract it such that the final value is: 0xa1df7 I have only been able to extract and get it working if the hex values that have at least 1 zero: unsigned char const data[ ] = {0x0a, 0xd0, 0xf0, 0x07}; using...

Why does the right-shift operator produce a zero instead of a one?

Hi, i am teaching myself java and i work through the exercises in Thinking in Java. On page 116, exercise 11, you should right-shift an integer through all its binary positions and display each position with Integer.toBinaryString. public static void main(String[] args) { int i = 8; System.out.println(Integer.toBinaryString(i)); int ma...

Shift count negative or too big error - correct solution?

I have the following function for reading a big-endian quadword (in a abstract base file I/O class): unsigned long long File::readBigEndQuadWord(){ unsigned long long qT = 0; qT |= readb() << 56; qT |= readb() << 48; qT |= readb() << 40; qT |= readb() << 32; qT |= readb() << 24; qT |= readb() << 16; qT |= readb() << 8; ...