bit-shift

Perform logical shift using arithmetic shift operator in C [duplicate]

那年仲夏 提交于 2019-11-29 17:28:20
This question already has an answer here: Implementing Logical Right Shift in C 8 answers Right now I am reading the book Computer Systems : Programmer Perspective. One problem in the book says to perform a logical right shift on a signed integer, I can't figure out how to start on this. The following is the actual question from the book: Fill in code for the following C functions. Function srl performs a logical right shift using an arithmetic right shift (given by value xsra ), followed by other operations not including right shifts or division. Function sra performs an arithmetic right

How is shift operator evaluated in C?

泄露秘密 提交于 2019-11-29 16:12:31
问题 I recently noticed a (weird) behavior when I conducted operations using shift >> << ! To explain it, let me write this small runnable code that does two operations which are supposed to be identical(In my understanding), but I'm surprised with different results! #include <stdio.h> int main(void) { unsigned char a=0x05, b=0x05; // first operation a = ((a<<7)>>7); // second operation b <<= 7; b >>= 7; printf("a=%X b=%X\n", a, b); return 0; } When ran, a = 5 and b = 1 . I expect them both to be

Time Complexity of a loop that integer divides the loop counter by a constant

↘锁芯ラ 提交于 2019-11-29 16:08:00
I'm trying to calculate the time complexity of a simple algorithm in big O notation, but one part of it is seriously boggling my mind. Here is a simplified version of the algorithm: int a=n while(a>0) { //for loop with time complexity n^3 a = a/8 } In this instance, it's integer division, so the while loop will terminate after a's value drops below 8. I'm not sure how to express this in terms of n. I'd also like to know how to tackle future calculations like this, where the number of loops isn't too easy to define. I find it easier to do it the other way around in cases like this. What is the

unexpected behavior of bitwise shifting using gcc

烂漫一生 提交于 2019-11-29 15:44:48
I have a test program like this: int main() { unsigned n = 32; printf("ans << 32 = 0x%X\n", (~0x0U) << 32); printf("ans >> 32 = 0x%X\n", (~0x0U) >> 32); printf("ans << n(32) = 0x%X\n", (~0x0U) << n); printf("ans >> n(32) = 0x%X\n", (~0x0U) >> n); return 0; } It produces the following output: ans << 32 = 0x0 ... (1) ans >> 32 = 0x0 ... (2) ans << n(32) = 0xFFFFFFFF ... (3) ans >> n(32) = 0xFFFFFFFF ... (4) I was expecting (1) and (3) to be the same, as well as (2) and (4) to be the same. Using gcc version: gcc.real (Ubuntu 4.4.1-4ubuntu9) 4.4.1 What is happening? Shifting by the size of the

Signed extension from 24 bit to 32 bit in C++

纵饮孤独 提交于 2019-11-29 14:32:57
I have 3 unsigned bytes that are coming over the wire separately. [byte1, byte2, byte3] I need to convert these to a signed 32-bit value but I am not quite sure how to handle the sign of the negative values. I thought of copying the bytes to the upper 3 bytes in the int32 and then shifting everything to the right but I read this may have unexpected behavior. Is there an easier way to handle this? The representation is using two's complement. You could use: uint32_t sign_extend_24_32(uint32_t x) { const int bits = 24; uint32_t m = 1u << (bits - 1); return (x ^ m) - m; } This works because: if

Understanding bitwise operations and their application in Java

点点圈 提交于 2019-11-29 12:07:15
I think understand what they fundamentally do - operate on bits (flip, shift, invert, etc...). My issue is that I don't know when I'd ever need to use them, and I don't think I fully understand bits. I know that there are 8 bits in a byte and I know that bits are either a 0 or 1 . Now here is where I start to get confused... I believe data types define combinations of bits differently. So if I declare an int , 32 bits are set aside for numbers, if I declare a char, 8 bits are set aside and the bits for that data type define a letter. Running with that idea, I did the following basic operation

Undefined behavior of right-shift in C++

你离开我真会死。 提交于 2019-11-29 11:54:21
问题 From cppreference.com: For unsigned a and for signed a with nonnegative values, the value of a >> b is the integer part of a/2 b . For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative). In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined. Why do we have an undefined behavior in

Shifted by negative number in java

老子叫甜甜 提交于 2019-11-29 11:23:14
I have problem with shift operator in Java.I have used following code and not unable to understand how this program generates this output.So please guide me how this program generates this output. public class Operator { public static void main(String[] args) { // TODO Auto-generated method stub int s = 8; s = s >>-63; System.out.println("value of i=" + s); } } Output: value of i=4 From the JLS, section 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. It is as if the right

Shifting a Java BitSet

自闭症网瘾萝莉.ら 提交于 2019-11-29 11:08:18
问题 I am using a java.util.BitSet to store a dense vector of bits. I want to implement an operation that shifts the bits right by 1, analogous to >>> on ints. Is there a library function that shifts BitSet s? If not, is there a better way than the below? public static void logicalRightShift(BitSet bs) { for (int i = 0; (i = bs.nextSetBit(i)) >= 0;) { // i is the first bit in a run of set bits. // Set any bit to the left of the run. if (i != 0) { bs.set(i - 1); } // Now i is the index of the bit

Nested templates vs shift operator

隐身守侯 提交于 2019-11-29 11:01:58
I have been reading all around about be aware >> as ending of nested template and >> as shift operator... Now I have tried it in my MSVS2010 and no problem occured. std::map<int, std::pair<int, int>> m; This code works exactly what I want (map of pairs) but I supposed to get some error about >> Compiler is smarter these days? MSVC++2010 supports C++0x feature Right Angle Brackets Be careful because previously good C++03 code may break with compilers supporting this feature. MyArray< MyArray<int, 16 >> 2>, 5 > arrayInst; This would be the fix: MyArray< MyArray<int, (16 >> 2)>, 5 > arrayInst;