bit

全局唯一Id:雪花算法

妖精的绣舞 提交于 2019-12-25 07:44:10
分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。 有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。 而twitter的SnowFlake解决了这种需求,最初Twitter把存储系统从MySQL迁移到Cassandra,因为Cassandra没有顺序ID生成机制,所以开发了这样一套全局唯一ID生成服务。 原理 Twitter的雪花算法SnowFlake,使用Java语言实现。 SnowFlake算法产生的ID是一个64位的整型,结构如下(每一部分用“-”符号分隔): 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 1位标识部分 ,在java中由于long的最高位是符号位,正数是0,负数是1,一般生成的ID为正数,所以为0; 41位时间戳部分 ,这个是毫秒级的时间,一般实现上不会存储当前的时间戳,而是时间戳的差值(当前时间-固定的开始时间),这样可以使产生的ID从更小值开始;41位的时间戳可以使用69年,(1L << 41) / (1000L 60 60 24 365) = 69年; 10位节点部分

ffmpeg所有的解码器(decoders)

老子叫甜甜 提交于 2019-12-25 07:36:53
FFMPEG解码器 Decoders : V . . . . . = Video ( 视频流) A . . . . . = Audio(音频流) S . . . . . = Subtitle(字幕流) . F . . . . = Frame - level multithreading . . S . . . = Slice - level multithreading . . . X . . = Codec is experimental . . . . B . = Supports draw_horiz_band . . . . . D = Supports direct rendering method 1 -- -- -- V . . . . D 012 v Uncompressed 4 : 2 : 2 10 - bit V . . . . D 4 xm 4 X Movie V . . . . D 8 bps QuickTime 8 BPS video V . . . . D aasc Autodesk RLE VF . . . D aic Apple Intermediate Codec V . . . . D alias_pix Alias / Wavefront PIX image V . . . . D amv AMV Video V . . . . D anm

Convert between formats with different number of bits for exponent and fractional part

▼魔方 西西 提交于 2019-12-25 02:38:28
问题 I am trying to refresh on floats. I am reading an exercise that asks to convert from format A having: k=3, 4 bits fraction and Bias=3 to format B having k=4, 3 bits fraction and Bias 7. We should round when necessary. Example between formats: 011 0000 (Value = 1) =====> 0111 000 (Value = 1) 010 1001 (Value = 25/32) =====> 0110 100 (Value = 3/4 Rounded down) 110 1111 (Value = 31/2) =====> 1011 000 (Value = 16 Rounded up) Problem: I can not figure out how the conversion works. First of all I

Reading and writing 64bits by 64 bits in C

我的未来我决定 提交于 2019-12-25 02:07:16
问题 I have this super simple code where I read blocks of 8 bytes (I will encrypt them later in the code) and then write them down in a new file. It works well but for the last 8 bytes which don't get written. Any idea why? #include <stdbool.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> int main() { uint64_t data; FILE *input, *output; // create output file output = fopen("output.txt", "w"); // read file input = fopen("test.txt", "rb"); if(input) { while(fread(&data, 8, 1, input) =

Reading binary file bitwise in R

牧云@^-^@ 提交于 2019-12-24 15:26:02
问题 Since I am not a programmer I got stuck solving this probably trivial problem: I would like to extract numeric values from binary file (example.Lis) using R. First 256 bytes contain a header. I know the header structure therefore I was able to decode its content. For instance, according the format description, OLE DATE information is stored in bytes 8-15 as a double data type. To read this, I simply used following command: my.file = file("example.Lis", "rb") headall<-readBin(my.file, raw(), n

Checking specific bits of a bitmask

一笑奈何 提交于 2019-12-24 10:23:03
问题 I am working with Bitmasks in python . As far as I know, these are arrays of integers that when they are unpacked into binary format they tell you which of the 32 bits are set (=1) for a given element in the array. I would like to know the fastest way to check whether 4 specific bits are set or not for any element of an array. I do not care about the rest. I have tried the following solution but it is not fast enough for my purpose: def detect(bitmask, check=(18,22,23,24), bits=32): boolmask

Accessing the bits in char through a bitfield

假装没事ソ 提交于 2019-12-24 07:25:06
问题 I want to access the bits in a char individually. There are several questions and answers on this topic here on SO, but they all suggest to use boolean mathematics. However, for my use it would be more convenient if I could simply name the bits separately. So I was thinking of just accessing the char through a bitfield, like so #include <stdbool.h> #include <stdio.h> typedef struct { bool _1 : 1, _2 : 1, _3 : 1, _4 : 1, _5 : 1, _6 : 1, _7 : 1, _8 : 1; } bits; int main() { char c = 0; bits *b

GCC Bit-scan-forward to find next set bit?

笑着哭i 提交于 2019-12-24 05:47:15
问题 I have a uint64_t and I would like to find the index of the first set bit, reset it to zero and find the next set bit. How do I know when to terminate? BSF on all zeros is undefined... const uint64_t input = source; if(0 != input){ int32_t setIndex = GCC_BSF_INTRINSIC(input); while(setIndex != UNDEFINED???){ //Do my logic //Reset input[setIndex] = 0; setIndex = BSF_Variant(input); } } Could somebody please help? 回答1: The simplest would be to just check the input: while (input) { int32_t index

bit shift multiplication in c not using powers of 2 [duplicate]

China☆狼群 提交于 2019-12-24 03:51:40
问题 This question already has answers here : How can I multiply and divide using only bit shifting and adding? (13 answers) Closed 6 years ago . How can I perform multiplication by 36 using bit-shifting? Isn't it only possible to multiply by powers of 2? For example: unsigned x = 4; // binary 00000000 00000000 00000000 00001000 unsigned y = x << 3; // multiply by 8, resulting in binary 00000000 ... 00100000 Thanks! 回答1: You can't multiply by a non-power of 2 by bit shifting alone. But you can

Rational comparison of bits

佐手、 提交于 2019-12-24 03:26:13
问题 I have a number of type int. It lies within [0,255]. That is, includes 8 bits. I need to check often say: 2(int) = 00000010(Binary) 1. The bit 6 and bit 7 must be equal to 0 and 1 respectively. And I check it like this: if ((!(informationOctet_ & (1 << 6))) && (informationOctet_ & (1 << 7))) { ... } But it is not very readable, whether it is possible - to do something "beautiful"? I can not use the std::bitset, my head says it's a waste of resources and you can not do without it. 回答1: There