bit-shift

Compute signed long max value in C using bit shift

不想你离开。 提交于 2019-12-08 07:14:54
问题 Just started learning C yesterday and this is about to drive me crazy on the new year's day... Try to print the different int ranges using bit shift operations. Everything works fine apart from the signed long max/min value. Can't figure out why (1 << 63) - 1 returns -1 ? But (1 << 64) -1 for unsigned long long works fine... #include <limits.h> #include <stdio.h> void print_range() { signed char scmax = (1 << 7) - 1; char c = scmax; // char means signed char! unsigned char uscmax = (1 << 8) -

Arithmetic Left shift time complexity

社会主义新天地 提交于 2019-12-08 07:14:24
问题 What is the time complexity of * Arithmetic left shift* / * Arithmetic right shift* operators of a n bit operand for example doing x = y << 2; whow much time will it take ? 回答1: Complexity, with the O(…) notation, is an asymptotic characterization of the time an algorithm takes when the input size becomes larger and larger. It is meaningless for algorithms that can take only a finite number of inputs. << can take 2^32 * 32 different inputs, hence a finite number of inputs, therefore it is

circular shift c

雨燕双飞 提交于 2019-12-08 06:32:54
问题 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) { 回答1: C does not have a circular shift, so I guess the exercise is to implement it. The way to do this for a left circular shift is to: - get the current leftmost bit and save it - shift the number leftwards by one - or the saved bit in at

Converting to Binary using bitwise and bitshift

北城以北 提交于 2019-12-08 06:09:05
问题 I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code. void PrintInBinary( unsigned int decNum ) { int i = 0; unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1); for( i = 0; i < sizeof(int)*8; i++ ) { printf( "%u", decNum & (highestOne >> i) ); } printf("\n"); } int main() { unsigned int a = 128; PrintInBinary( a ); system("PAUSE"); return 0; } The following is the output:

Undoing shifts without truncating

喜夏-厌秋 提交于 2019-12-08 05:13:13
问题 I'm a bit baffled by this. Shouldn't the values truncate after the shift? Does anyone know why this happens? long a, b, c, n; //assign any value to a, set b and c to 0x000...0 n = 128; //any number works; b = a << n; c = b >> n; a == (b >> n); // True a == c; //True; Postscript I've always understood that if you shift a buffer in any direction, the values that "fall" outside the buffer size get truncated, and that they're essentially lost unless you get them from the original buffer. Thats

Circular Left Rotation Algorithm in C#

痴心易碎 提交于 2019-12-08 04:05:40
问题 SO I have this algorithm that does left rotation public static ushort RotateLeft(ushort value, int count) { int left = value << count; int right = value >> (16 - count); return (ushort)(left | right); } However this is not producing the values I want for example if I have a value of 18 when rotated left by 1 bit it the result should be 3 but instead this just adds a zero at the end: This is what the algorithm does: 10010 18 100100 36 This is what I want: 10010 18 00101 3 The bits should be

Use Perl to Add GIF Image Other Than 8-bit to PDF

≯℡__Kan透↙ 提交于 2019-12-08 01:25:32
I am attempting to add non-interlaced GIF images other than 8-bit to a PDF document without having to fully decode the bitstream using PDF::Create for Perl. The LZWDecode algorithm that is part of the PDF standard requires all images to have a minimum LZW code size of 8-bits, and PDF::Create is hard-coded to only embed 8-bit images. So far, I have adapted the image loader from PDF::Create to read a 5-bit image and to fully decode the LZW stream. I am then able to use the encoder algorithm from PDF::Create to re-pack the image as 8-bit. What I'd like to do is to eliminate the memory-intensive

14-bit left-justified two's complement to a signed short

霸气de小男生 提交于 2019-12-07 17:07:39
问题 I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?) What would be the fastest way to do that? 回答1: Simply divide by 4. (Note, right-shift leads to implementation/undefined behaviour.) 回答2: A portable solution: short convert(unsigned char hi, unsigned char lo) { int s = (hi << 6) | (lo >> 2); if (s >= 8192) s -= 16384; return s; } 来源: https://stackoverflow.com/questions/14710764/14

Unwanted Java bitshift behavior

落爺英雄遲暮 提交于 2019-12-07 13:00:51
问题 I'm generating bitmasks for some calculations I'm doing, where I need to mask an int such that all except the x rightmost bits become zero. I do this using: int mask = ~(-1 << x); This works fine for all values of x except for x = 32. It should return -1 then, but it returns 0. What is happening here? Also, I tried this: int mask = -1 >>> 32 - x; At x = 0 it should return 0, yet it returns -1. Somehow shifting something by 32 causes the operation to return the left side of the operator. When

Applications of a circular shift

邮差的信 提交于 2019-12-07 12:49:46
问题 I would like to know some examples of application of circular shifts. For example, a right shift on an unsigned integer would result in a division by two. Conversely, a left shift would result in a multiplication by 2. Are there any famous/interesting properties of a circular shift on binary numbers. Note: The example about the right/left shift is to illustrate an application of that particular operator. I am asking for similar examples for the circular shift operator/function. 回答1: Convert a