bit

sign extension in C

a 夏天 提交于 2019-12-06 12:24:00
I'm looking here to understand sign extension: http://www.shrubbery.net/solaris9ab/SUNWdev/SOL64TRANS/p8.html struct foo { unsigned int base:19, rehash:13; }; main(int argc, char *argv[]) { struct foo a; unsigned long addr; a.base = 0x40000; addr = a.base << 13; /* Sign extension here! */ printf("addr 0x%lx\n", addr); addr = (unsigned int)(a.base << 13); /* No sign extension here! */ printf("addr 0x%lx\n", addr); } They claim this: ------------------ 64 bit: % cc -o test64 -xarch=v9 test.c % ./test64 addr 0xffffffff80000000 addr 0x80000000 % ------------------ 32 bit: % cc -o test32 test.c % .

mysql高级

淺唱寂寞╮ 提交于 2019-12-06 11:48:16
1.UNSIGNED无符号类型 ZEROFILL用0填充以满足数据长度。 2.bit_count(data):data的二进制数包含1的个数;mysql> select bit_count(3);结果为2 bit_or(): 按位或运算 bit_and():按位与运算 例如: CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL, day INT(2) UNSIGNED ZEROFILL); INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2), (2000,2,23),(2000,2,23); 求上表中:每月的访问量? SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1 GROUP BY year,month;| 由于1<<day 如果day数据相同,那么运算后的结果是一样的。bit_or运算后对应位1和1或运算结果还是1.这样就达到了去重的效果。 来源: https://www.cnblogs.com/chengxuyuan-liu/p/11981474.html

Does the type of bitfield affect structure alignement

半腔热情 提交于 2019-12-06 11:37:04
I have the following structures: struct bf_struct1 { uint64_t bf1 : 1; uint64_t bf2 : 6; uint64_t bf3 : 2; uint64_t bf4 : 55; } struct bf_struct2 { uint8_t bf1 : 1; uint8_t bf2 : 6; uint8_t bf3 : 2; uint64_t bf4 : 55; } Does the structure member alignment depend on type of a bitfield members? From the horse's mouth : 6.7.2.1 Structure and union specifiers ... 5 A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int , unsigned int , or some other implementation-defined type. It is implementation-defined whether atomic types are permitted. ... 11 An

Bitwise Logic in C

你离开我真会死。 提交于 2019-12-06 11:34:12
I have some familiarity with bitwise operations, but this function just went over my head. void binary_print(unsigned int value) { unsigned int mask = 0xff000000; // Start with a mask for the highest byte. unsigned int shift = 256*256*256; // Start with a shift for the highest byte. unsigned int byte, byte_iterator, bit_iterator; for (byte_iterator=0; byte_iterator < 4; byte_iterator++) { byte = (value & mask) / shift; // Isolate each byte. printf(" "); for (bit_iterator=0; bit_iterator < 8; bit_iterator++) { // Print the byte's bits. if (byte & 0x80) // If the highest bit in the byte isn't 0,

Write one single bit to binary file using BinaryWriter

左心房为你撑大大i 提交于 2019-12-06 09:59:07
I want to write one single bit to a binary file. using (FileStream fileStream = new FileStream(@"myfile.bin", FileMode.Create)) using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) { binaryWriter.Write((bool)10); } Something like binaryWriter.Write((bit)1); When I use binaryWriter.Write((bool)1) the file has one byte, but I want to write one single bit. Is this possible? You cannot store only 1 bit in a file. Almost all modern filesystems and hardware store data in segments of 8 bits, aka bytes or octets. If you want store a bit value in a file, store either 1 or 0 as a byte

分库分表之后,ID 主键如何处理?

人盡茶涼 提交于 2019-12-06 09:41:46
分库分表之后,ID 主键如何处理? 面试题 分库分表之后,id 主键如何处理? 面试官心理分析 其实这是分库分表之后你必然要面对的一个问题,就是 id 咋生成?因为要是分成多个表之后,每个表都是从 1 开始累加,那肯定不对啊,需要一个 全局唯一 的 id 来支持。所以这都是你实际生产环境中必须考虑的问题。 面试题剖析 基于数据库的实现方案 数据库自增 id 这个就是说你的系统里每次得到一个 id,都是往一个库的一个表里插入一条没什么业务含义的数据,然后获取一个数据库自增的一个 id。拿到这个 id 之后再往对应的分库分表里去写入。 这个方案的好处就是方便简单,谁都会用; 缺点就是单库生成 自增 id,要是高并发的话,就会有瓶颈的;如果你硬是要改进一下,那么就专门开一个服务出来,这个服务每次就拿到当前 id 最大值,然后自己递增几个 id,一次性返回一批 id,然后再把当前最大 id 值修改成递增几个 id 之后的一个值;但是 无论如何都是基于单个数据库 。 适合的场景 :你分库分表就俩原因,要不就是单库并发太高,要不就是单库数据量太大;除非是你 并发不高,但是数据量太大 导致的分库分表扩容,你可以用这个方案,因为可能每秒最高并发最多就几百,那么就走单独的一个库和表生成自增主键即可。 设置数据库 sequence 或者表自增字段步长 可以通过设置数据库 sequence

Swift - Convert UInt8 byte to array of bits

六眼飞鱼酱① 提交于 2019-12-06 08:16:22
I'm trying to decode a protobuff encoded message, so I need to convert the first byte (the key) in the protobuff message into bits, so I can find the field number. How do I convert a UInt8 (the byte) into an array of bits? Pseudo Code private func findFieldNum(from byte: UInt8) -> Int { //Byte is 0001 1010 var fieldNumBits = byte[1] ++ byte[2] ++ byte[3] ++ byte[4] //concatentates bits to get 0011 getFieldNum(from: fieldNumBits) //Converts 0011 to field number, 2^1 + 2^0 = 3 } I saw this question , which converts an array of bits into array of bytes. Here's a basic function to get a Bit array

Generate bit vector with t random ones

假装没事ソ 提交于 2019-12-06 07:18:54
I want to generate an array/vector v of size p with t ones and p-t zeros. The position of the t ones must be random. This is the solution that I've written so far, but I'm not sure if it's the most efficient one. In addition I never used random_device or mt19937 (found them here ) before, so I don't know what are the possible drawbacks. #include <algorithm> #include <random> #include <vector> ... int p=10, t=3; std::vector<int> v(p,0); for(int i=0;i<t;i++) //better way? v[i] = 1; std::random_device rd; std::mt19937 g(rd()); std::shuffle(std::begin(v), std::end(v), g); This is the matlab code

多属性、多分类MySQL模式设计

陌路散爱 提交于 2019-12-06 06:43:47
一、导读 这是来自B乎的一个问答。 当数据同时具备多个属性/分类时,改如何设计表结构和查询? 二、需求描述 我偶尔也会逛逛B乎,看到一些感兴趣的话题也会回复下。 有一次,看到这样的一个话题: 链接: https://www.zhihu.com/questio... [mysql] 当数据同时属于多个分类时,该怎么查询? 分类cate字段为[1,2,3,4,5] ,假如要查询满足分类’2’和’5′ 的数据该怎么查询? 我尝试过用 cate like ‘%2%’ AND cate like ‘%5%’去查。 想问有没有更好的办法,我这样写数据少了还好,多了根本没法查,效率太低了。 恰好我以前做过类似的业务需求设计,所以就回复了这个问题。 三、模式设计思路 这个需求可以有几种不同的解决思路,我们分别展开说一下。 (一)用bit数据类型 大概思路如下: 1、物品属性列c1 用bit数据类型 来表示,也就是只有0、1两种取值 2、当物品属性具备某个分类属性时,其值为1,否则为0 3、假如共有5个分类,当物品拥有全部分类属性时,则其值为11111,若其不具备第3个分类属性,则其值为11011,在数据库中转成十进制存储 4、上述两种情况下,将二进制转换成十进制表示,即分别是31和27 [root@yejr.me] [zhishutang]> select conv(11111, 2, 10),

【LOJ #6041】「雅礼集训 2017 Day7」事情的相似度

你说的曾经没有我的故事 提交于 2019-12-06 06:36:32
Problem LOJ #6041「雅礼集训 2017 Day7」事情的相似度 给定一个长度为 \(n\) 的 \(01\) 串 \(s\) ,有 \(m\) 次询问,每次询问给定了一个区间 \([l,r](l < r)\) ,你需要找到最大的 \(t\) ,使得存在两个整数 \(i,j(l \leq i < j \leq r)\) , \(s[1..i]\) 和 \(s[1..j]\) 的最长公共后缀的长度为 \(t\) 。允许离线。 \(n,m \leq 10^5\) 。 时间限制: \(3 \texttt s\) 内存限制: \(1024 \texttt{MB}\) Solution 算法一:SA(M) + 莫队(部分分) 把字符串取反,那么就相当于询问区间内的后缀,两两之间 LCP 的最大值。 考虑离线后莫队,莫队的时候维护一个 set 表示当前区间的所有点的 rank,再维护一个 multiset 表示在前一个 set 中所有相邻两点的 LCP。 视 \(n,m\) 同阶,时间复杂度 \(\mathcal O(n \sqrt n \log n)\) 。非常可惜,通过不了本题。 用 SAM 实现也是可以的,LCP 对应过去就是 parent 树 LCA 的 maxl(maxl 表示后缀自动机中某个点表示子串的最大长度)。 算法二:SA(M) + bitset(骚解)