bit-shift

Parsing webp file header in Kotlin to get its height and width, but getting unexpected results

有些话、适合烂在心里 提交于 2020-11-29 19:27:49
问题 I am trying to read the WebP image header, according to the WebP Container Specification of Extended File Format. fun get24bit(data: ByteArray, index: Int): Int { return ((data[0 + index].toInt()) or (data[1 + index].toInt() shl 8) or (data[2 + index].toInt() shl 16)) } fun get32bit(data: ByteArray, index: Int): Int { return get24bit(data, index) or (data[3 + index].toInt() shl 24) } // data -> File(fileName).readBytes() for testing purpose fun webpExtract(data: ByteArray) { println(String

What is (x & 1) and (x >>= 1)?

我只是一个虾纸丫 提交于 2020-08-17 12:41:26
问题 I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer to a bit representation shows something like this: do { Vec.push_back( x & 1 ) } while ( x >>= 1 ); I don't want to just copy paste stuff. When I use F-10 I see what (x & 1) is doing but I don't know it is name or how it does its job(compare

Converting from RGB ints to Hex

你说的曾经没有我的故事 提交于 2020-08-01 09:31:28
问题 What I have is R:255 G:181 B:178, and I am working in C# (for WP8, to be more specific) I would like to convert this to a hex number to use as a color (to set the pixel color of a WriteableBitmap). What I am doing is the following: int hex = (255 << 24) | ((byte)R << 16) | ((byte)G << 8) | ((Byte)B<<0); But when I do this, I just get blue. Any ideas what I am doing wrong? Also, to undo this, to check the RGB values, I am going: int r = ((byte)(hex >> 16)); // = 0 int g = ((byte)(hex >> 8)); /

What is 0xFF and why is it shifted 24 times?

橙三吉。 提交于 2020-07-04 07:22:34
问题 #define SwapByte4(ldata) \ (((ldata & 0x000000FF) << 24) | \ ((ldata & 0x0000FF00) << 8) | \ ((ldata & 0x00FF0000) >> 8) | \ ((ldata & 0xFF000000) >> 24)) What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24? 回答1: Here is a hex value, 0x12345678, written as binary, and annotated with some bit positions: |31 24|23 16|15 8|7 bit 0| +---------------+---------------+---------------+---------------+ |0 0 0 1 0 0 1 0|0 0 1 1 0 1 0 0|0 1 0 1 0 1

What is 0xFF and why is it shifted 24 times?

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-04 07:21:21
问题 #define SwapByte4(ldata) \ (((ldata & 0x000000FF) << 24) | \ ((ldata & 0x0000FF00) << 8) | \ ((ldata & 0x00FF0000) >> 8) | \ ((ldata & 0xFF000000) >> 24)) What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24? 回答1: Here is a hex value, 0x12345678, written as binary, and annotated with some bit positions: |31 24|23 16|15 8|7 bit 0| +---------------+---------------+---------------+---------------+ |0 0 0 1 0 0 1 0|0 0 1 1 0 1 0 0|0 1 0 1 0 1

What is 0xFF and why is it shifted 24 times?

≡放荡痞女 提交于 2020-07-04 07:21:11
问题 #define SwapByte4(ldata) \ (((ldata & 0x000000FF) << 24) | \ ((ldata & 0x0000FF00) << 8) | \ ((ldata & 0x00FF0000) >> 8) | \ ((ldata & 0xFF000000) >> 24)) What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24? 回答1: Here is a hex value, 0x12345678, written as binary, and annotated with some bit positions: |31 24|23 16|15 8|7 bit 0| +---------------+---------------+---------------+---------------+ |0 0 0 1 0 0 1 0|0 0 1 1 0 1 0 0|0 1 0 1 0 1

Why does it make a difference if left and right shift are used together in one expression or not?

百般思念 提交于 2020-06-22 08:21:30
问题 I have the following code: unsigned char x = 255; printf("%x\n", x); // ff unsigned char tmp = x << 7; unsigned char y = tmp >> 7; printf("%x\n", y); // 1 unsigned char z = (x << 7) >> 7; printf("%x\n", z); // ff I would have expected y and z to be the same. But they differ depending on whether a intermediary variable is used. It would be interesting to know why this is the case. 回答1: This little test is actually more subtle than it looks as the behavior is implementation defined: unsigned

find if a number is divisible by 8 - using bit shifting operators

好久不见. 提交于 2020-06-09 18:01:05
问题 I was recently asked during an interview, using just bit shift operators, write some code that would tell you if a number is divisible by 8, apparently the code is very short - does anyone have a clue? Note: if you aren't required to use shifts, you'd test the low n bits for being all-zero with a mask like x&7 == 0 , or more generally x & ((1UL << n) - 1) == 0 . How can I tell if a number is a multiple of four using only the logic operator AND? 回答1: With any integer represented in binary the

python mixing garmin timestamps

╄→尐↘猪︶ㄣ 提交于 2020-03-03 09:07:39
问题 I belong a garmin watch , to report statistic they have a sdk in this SDK they have a timestamp in two format one is a true timestamp on 32 bit another is the lower part on 16 bit which must be combinate whith the first I dont know to code this in Python Can somebody help me here is their explanation and the formula *timestamp_16 is a 16 bit version of the timestamp field (which is 32 bit) that represents the lower 16 bits of the timestamp. This field is meant to be used in combination with