bit-shift

Can I prevent a integer overflow in C# using an unsigned right shift?

I want alwaysPositive to be assigned a positive number with all possible values for lareValue1 and largeValue2 (these are at least 1). The following statement causes a buffer overflow: int alwaysPositive = (largeValue1 + largeValue2) / 2; I know I can prevent it by substracting and adding: int alwaysPositive = largeValue1 + ((largeV...

Absolute Beginner's Guide to Bit Shifting?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators) ... What I'm wondering is, at a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it help solve, and what gotchas lurk around the bend? In other words, an absolute beginner's g...

How can I turn an int into three bytes in Java?

I am trying to convert an int into three bytes representing that int (big endian). I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it. For example: int myInt; // some code byte b1, b2 , b3; // b1 is most significant, then b2 then b3. *Note, I am aware that an int is 4 b...

K & R Question: Need help understanding "getbits()" method in Chapter 2

As I've mentioned before, I'm going through K&R, and overall am doing all right with it. However, in chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works -- and as a result, I'm having trouble with the associated exercises. (This isn't a dupe of my prior questio...

Bit Operators

If I have two things which are hex, can I someone how append their binary together to get a value? In C++, say I have unsigned char t = 0xc2; // 11000010 unsigned char q = 0xa3; // 10100011 What I want is somehow, 1100001010100011, is this possible using bit-wise operators? I want to extract the binary form of t and q and append t...

Is the binary data I convert to a Short valid?

Howdy, I am reading a binary log file produced by a piece of equipment. I have the data in a byte[]. If I need to read two bytes to create a short I can do something like this: short value = (short)(byte[1] << 8); value += byte[2]; Now I know the value is the correct for valid data. How would I know if the file was messed up...

How can I concatenate upper and lower nibbles stored sequentially in an array?

Hi friends, I have an array which contains a list of nibbles: {0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, ...} I want to combine adjacent nibbles into single bytes by left-shifting the upper nibble and concatenating it with the lower one. The output should look as follows: {0xab, 0xcd, 0xef, ...} How can I accomplish this in C? ...

In C Programming, can I use multiply and divide to shift bits?

Instead of using >> and << to shift, is it posible to use * and / to shift left and right? for an 8-bit: 0x01*2 = 0000|0010? ...

Have you ever had to use bit shifting in real projects?

Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to use them? ...

Why does this bitwise shift-right appear not to work?

Could someone explain to me why the mask is not shifted to the right at all? You can use anything in place of that 1 and the result will be the same. unsigned mask = ~0 >> 1; printf("%u\n", mask); ...

Why use only the lower five bits of the shift operand when shifting a 32-bit value? (e.g. (UInt32)1 << 33 == 2)

Consider the following code: UInt32 val = 1; UInt32 shift31 = val << 31; // shift31 == 0x80000000 UInt32 shift32 = val << 32; // shift32 == 0x00000001 UInt32 shift33 = val << 33; // shift33 == 0x00000002 UInt32 shift33a = (UInt32)((UInt64)val << 33); // shift33a == 0x00000000 ...

Left bit shifting 255 (as a byte)

Can anyone explain why the following doesn't compile? byte b = 255 << 1 The error: Constant value '510' cannot be converted to a 'byte' I'm expecting the following in binary: 1111 1110 The type conversion has stumped me. ...

Shift operation on MC68000

I'm using shift operations to divide a long word in register D3 by three and storing the result in memory address specified by A2: MOVE.L D1, D2, D3, -(SP) CLR.L D5 MOVE.B #1, D4 Loop LSR.L D2, D3 ROXR.L #1,D5 BCS DONE SUBI.B #1, D2 BLT Loop CLR.B D4 ...

There is any C# bitwise shift operator that moves the overflown bits to the other tip of the variable?

Let's call it "<<<" int32variable <<< numberOfBits equals (int32variable << numberOfBits) | (int32variable >> (32 - numberOfBits)) (Assuming << and >> discards overflown bits) There is such an operator? ...

Will bit-shift by zero bits work correctly?

Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int. I'm particularly concerned about call with bitCount equal to zero - will it work correctly then? Also is ther...

bitwise operators and "Endianness"

Does Endianness matter at all with the bitwise operations. Either logical or shifting? I'm working on homework with regard to bitwise operators and I can not make heads or tails on it and I think I'm getting quite hung up on the endianess. That is, I'm using a little endian machine (like most are) but does this need to be considered or...

AS3 bitwise shift 0?

I came across this in some AS 3.0 code: (duration >> 0) Where duration is a Number. I think I know what a bitwise right shift does, but what's the point of shifting 0 bits? This happens a few times in the code, I'd like to understand what it does. ...

(-1 >> 1) == -1 - Why?

Why does (-1 >> 1) result in -1? I'm working in C, though I don't think that should matter. I can not figure out what I'm missing... Here is an example of a C program that does the calc: #include <stdio.h> int main() { int num1 = -1; int num2 = (num1 >> 1); printf( "num1=%d", num1 ); printf( "\nnum2=%d", num2 ); ...

What does this line of code do ?

Hi Confused as to what this code does for (L=0; L < levels; L++, N_half>>=1){ func( y, N_half); } // end: levels for loop In particular this " N_half>>=1 " Thanks ...

Java: Checking if a bit is 0 or 1 in a long

Given that : 0000000000000000000000000000000000000000000000000000000000000001 = 1 What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ? ...