bit-manipulation

what does it mean to bitwise left shift an unsigned char with 16

可紊 提交于 2019-12-21 07:55:58
问题 i am reading a .cpp file containing a unsigned char variable, it's trying the bitwise left shift 16 bits, since an unsigned char is composed of 8 bits, left shift 16 bits will erase all the bits and fill it with eight 0s. unsigned char byte=0xff; byte << 16; 回答1: When you shift a value, unsigned char x = ...; int y = x << 16; The type of x is promoted to int if unsigned char fits in an int (most systems), or to unsigned if unsigned char does not fit in an int (rare 1 ). As long as your int is

What does >> mean in PHP?

ε祈祈猫儿з 提交于 2019-12-21 07:08:26
问题 Consider: echo 50 >> 4; Output: 3 Why does it output 3? 回答1: 50 in binary is 11 0010 , shift right by 4 yields 11 which is equal to 3. See PHP documentation and Wikipedia. 回答2: As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right: $a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two") 50 in binary is 110010 , and the >> operator shifts those bits over 4 places in your example code. Although this happens in a

What does >> mean in PHP?

醉酒当歌 提交于 2019-12-21 07:08:24
问题 Consider: echo 50 >> 4; Output: 3 Why does it output 3? 回答1: 50 in binary is 11 0010 , shift right by 4 yields 11 which is equal to 3. See PHP documentation and Wikipedia. 回答2: As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right: $a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two") 50 in binary is 110010 , and the >> operator shifts those bits over 4 places in your example code. Although this happens in a

What does i+=(i&-i) do? Is it portable?

心已入冬 提交于 2019-12-21 07:04:47
问题 Let i be a signed integer type. Consider i += (i&-i); i -= (i&-i); where initially i>0 . What do these do? Is there an equivalent code using arithmetic only? Is this dependent on a specific bit representation of negative integers? Source: setter's code of an online coding puzzle (w/o any explanation/comments). 回答1: If i has unsigned type, the expressions are completely portable and well-defined. If i has signed type, it's not portable, since & is defined in terms of representations but unary

Generate random int with specified upper-bound (0 - n) without using / or %

瘦欲@ 提交于 2019-12-21 06:25:41
问题 roommate went to an interview and got this one: Rules: permitted to use rand(); RAND_MAX = 32 767; no use of division or modulo; TODO: Write a function that takes one int parameter and returns int in range 0 - parameter. Head hurts, can't sleep. Any help appreciated. Thanks 回答1: In my public domain randlib, I do it with no floating point, no division, no multiplication, just bitmasking and rejection sampling, like this: int ojr_rand(ojr_generator *g, int limit) { int v, m = limit - 1; m |= m

Concatenate binary numbers of different lengths

半城伤御伤魂 提交于 2019-12-21 05:26:06
问题 So I have 3 numbers. One is a char , and the other two are int16_t (also known as short s, but according to a table I found shorts won't reliably be 16 bits). I'd like to concatenate them together. So say that the values of them were: 10010001 1111111111111101 1001011010110101 I'd like to end up with a long long containing: 1001000111111111111111011001011010110101000000000000000000000000 Using some solutions I've found online, I came up with this: long long result; result = num1; result =

Bitwise operators and signed types

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 05:03:05
问题 I'm reading C++ Primer and I'm slightly confused by a few comments which talk about how Bitwise operators deal with signed types. I'll quote: Quote #1 (When talking about Bitwise operators) "If the operand is signed and its value is negative, then the way that the “sign bit” is handled in a number of the bitwise operations is machine dependent. Moreover, doing a left shift that changes the value of the sign bit is undefined" Quote #2 (When talking about the rightshift operator) "If that

C# Language: Changing the First Four Bits in a Byte

别来无恙 提交于 2019-12-21 04:16:27
问题 In order to utilize a byte to its fullest potential, I'm attempting to store two unique values into a byte: one in the first four bits and another in the second four bits. However, I've found that, while this practice allows for optimized memory allocation, it makes changing the individual values stored in the byte difficult. In my code, I want to change the first set of four bits in a byte while maintaining the value of the second four bits in the same byte. While bitwise operations allow me

Bitwise operations with CGBitmapInfo and CGImageAlphaInfo

醉酒当歌 提交于 2019-12-21 04:03:12
问题 I'm having trouble performing bitwise operations with CGImageAlphaInfo and CGBitmapInfo in Swift. In particular, I don't know how to port this Objective-C code: bitmapInfo &= ~kCGBitmapAlphaInfoMask; bitmapInfo |= kCGImageAlphaNoneSkipFirst; The following straightforward Swift port produces the somewhat cryptic compiler error 'CGBitmapInfo' is not identical to 'Bool' on the last line: bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask bitmapInfo |= CGImageAlphaInfo.NoneSkipFirst Looking at the source

How to create mask with least significat bits set to 1 in C

最后都变了- 提交于 2019-12-21 03:46:47
问题 Can someone please explain this function to me? A mask with the least significant n bits set to 1. Ex: n = 6 --> 0x2F, n = 17 --> 0x1FFFF // I don't get these at all, especially how n = 6 --> 0x2F Also, what is a mask? 回答1: The usual way is to take a 1 , and shift it left n bits. That will give you something like: 00100000 . Then subtract one from that, which will clear the bit that's set, and set all the less significant bits, so in this case we'd get: 00011111 . A mask is normally used with