bit-shift

Left shift operator

五迷三道 提交于 2019-12-11 10:47:31
问题 If I have the following: char v = 32; // 0010 0000 then I do: v << 2 the number becames negative. // 1000 0000 -128 I read the standard but it is only written: If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. so I don't understand if is a rule that if a bit goes on most left bit the number must begin negative. I'm using GCC. 回答1: Left shifting it twice would give 1000 0000)

C# - Convert ARGB Color to RGB555

删除回忆录丶 提交于 2019-12-11 06:39:49
问题 I have an algorithm that converts RGB555 values to a System.Drawing.Color object; public static Color ToColor(ushort color) { int a = color & 0x8000; int r = color & 0x7C00; int g = color & 0x03E0; int b = color & 0x1F; int rgb = (r << 9) | (g << 6) | (b << 3); return Color.FromArgb((a * 0x1FE00) | rgb | ((rgb >> 5) & 0x070707)); } A friend wrote this method for me (bitwise shifting is a bit over my head) , what would be the most efficient way to reverse this code? Thanks for any advice, I've

a macro to count bits that are on (set)

北城余情 提交于 2019-12-11 05:48:10
问题 My task is to write a macro that checks how many elements in an array of INTs has excatly 5 bits that are on. I know a macro is a very risky way of doing it, but that is the question that appears in some exams. This is my code: #include <stdio.h> #define RESULT 5 #define SIZE 8 #define BITW(arr, length, counter)\ int mask=0b00000001, bits=0, i=0, j=0;\ for (i=0; i<length; i++){\ for (j=0; j<sizeof(arr[i])*SIZE; j++){\ if(mask&arr[i]>>j)\ bits++;\ }\ if (bits==RESULT)\ counter++;\ } int main

Unwanted sign extension in Arduino

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:24:39
问题 I am trying to achieve logical right shift in Arduino(i.e. avoid sign extension), and after reading the Arduino BitShift guide (https://www.arduino.cc/en/Reference/Bitshift), it suggests that shifting unsigned variables to the right, wont cause sign extension: When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have

Implementation of logical right shift of negative numbers in c

℡╲_俬逩灬. 提交于 2019-12-11 04:15:52
问题 Is there a simple way to do logical right shift in c for negative numbers like how we have >> for arithmetic right shift? 回答1: Right-shifting negative numbers invoke implementation-defined behavior in C. What will happen is not specified by the standard, but left to the compiler to specify. So it can either result in an arithmetic shift, or a logical shift, or perhaps something else entirely (like rotation, though I've never heard of that). You can't know or assume which method that applies

Bit Shift Large Binary File?

廉价感情. 提交于 2019-12-10 23:13:38
问题 What is the best or recommended method for bit shifting a large amount of binary data in C? I have a 200K binary file and I want to left, then right shift the entire lot. 回答1: If your OS can support it use a memory mapped file. Then do a bit shift It'll be very very efficient. See this answer for more info: What are the advantages of memory-mapped files? 来源: https://stackoverflow.com/questions/8309801/bit-shift-large-binary-file

Bitshift operators description in Java language specification [closed]

不羁岁月 提交于 2019-12-10 22:57:17
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . At specified in JLS8 at §JLS-15.19 If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance . It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask

Undefined behaviour of right shift (a >> b) when b is greater than the number of bits in a?

折月煮酒 提交于 2019-12-10 17:39:44
问题 Apparently, the behaviour of the right shift operation: a >> b is undefined in C and C++ when b >= sizeof(a)*CHAR_BIT (whereas in the normal case, the "new bits" introduced from the left due to the right shift are equal to zero). Why is this undefined behaviour better than setting the result to zero when b >= sizeof(a)*CHAR_BIT ? 回答1: We can get an idea of why languages choose undefined behavior from Why Language Designers Tolerate Undefined Behavior and it says: This answer came from two

Is it expected that a too large bitshift is undefined behavior in Rust?

心已入冬 提交于 2019-12-10 14:17:39
问题 When you run this code: #![allow(exceeding_bitshifts)] fn main() { const NUMBER: u64 = 0b_10101010; fn print_shift(i: u32) { println!("{:b}", NUMBER >> i); } print_shift(65); println!("{:b}", NUMBER >> 65); } You see that shifting the bits of a number with a value that exceed the bit length produces different behavior when doing at compile time or runtime. Is it a normal behavior? Is it documented somewhere? This is not in the list of documented undefined behavior. 回答1: No, this is not

Bit-shifting left and discarding bits

♀尐吖头ヾ 提交于 2019-12-10 13:15:18
问题 Let's consider the function (one of possible implementations of it) which would zero out right N bits of an unsigned short value (or any other unsigned integral type). The possible implementation could look like following: template<unsigned int shift> unsigned short zero_right(unsigned short arg) { using type = unsigned short; constexpr type mask = ~(type(0)); constexpr type right_zeros = mask << shift; // <-- error here return arg & right_zeros; } int check() { return zero_right<4>(16); }