bit

How can I get first n bits of floating point number as integer in python

核能气质少年 提交于 2019-12-06 06:01:57
Suppose I have 0.625 as a floating point is 0b.101 , so if I want the first two bits of that as an integer i.e. 0b10 = 2 , how can I achieve this in python? I've tried taking the number to a power of 2 and casting to an int, so if I want n bits I do int(0.625*(2**n)) . But that is not working for me. The problem occurs when I have a number greater than 1 so 24.548838022726972 will give me 392 rather than 12 for the first four bits. ( 24.548838022726972 = 0b11000.100011001... ) If you want the n most significant bits, one way to start is to use math.frexp to normalise your number to lie in the

linux kernel switch driver(android headset device detection)

左心房为你撑大大i 提交于 2019-12-06 05:24:49
总结下linux kernel switch driver。 这里的switch driver是为监听CPU gpio口状态变化的,switch可以理解为gpio口状态变化。 switch driver是因android引入的。 下面以耳机接入为例说下androd系统中耳机接入检测的过程 kernel/arch/arm64/boot/dts/xxx.dts switch_gpio { compatible = "mstar,switch-gpio"; switch-name = "h2w"; switch-gpio = <66>; switch-inverse = <0>; }; 上面的switch-gpio是想要监听的CPU gpio index,用来告诉switch driver要监听的gpio口 解析上述dts switch_gpio section是在kernel switch driver中: drivers/switch/switch_gpio.c static int gpio_switch_probe(struct platform_device *pdev) { struct gpio_switch_platform_data *pdata = pdev->dev.platform_data; struct gpio_switch_data *switch

What does <<= operator mean in Java?

痞子三分冷 提交于 2019-12-06 04:01:55
问题 Can you please explain this code snippet from HashMap constructor specifically the line capacity <<= 1: // Find a power of 2 >= initialCapacity 198 int capacity = 1; 199 while (capacity < initialCapacity) 200 capacity <<= 1; 回答1: It is equivalent to capacity = capacity << 1; . That operation shifts capacity's bits one position to the left, which is equivalent to multiplying by 2. The specific code you posted finds the smallest power of 2 which is larger than initialCapacity . So if

Getting essid via ioctl in ruby

五迷三道 提交于 2019-12-06 03:44:01
To avoid relying on the wireless tools I want to get the essid directly from the device with ioctl, in C this wouldn't be a problem, but in Ruby it's quite different. The problem is following struct from wireless.h that is used as input/reply of ioctl: struct iw_point { void __user *pointer; /* Pointer to the data (in user space) */ __u16 length; /* number of fields or size in bytes */ __u16 flags; /* Optional params */ }; The pointer part must be a valid address of a memory area, followed by the length in bytes, followed by a flag field. I tried with Array#pack and the bit-struct gem, but

Setting Bits in C

血红的双手。 提交于 2019-12-06 03:29:30
问题 I'm trying to do the following: Write a func setbits(x,p.n,y) that returns x with n bits that begin at position p set to the rightmost n bits of y ,leaving the other bits unchanged? I tried it like this but not getting correct answers. Can anyone tell where I am wrong? unsigned setbits(unsigned x,int p,int n,unsigned y) { return (x>>p & (y|(~0<<n))); } 回答1: Something like: unsigned setbits(unsigned x,int p,int n,unsigned y) { unsigned mask = (1U << n) - 1U; // n-bits y &= mask; // rightmost n

python_掷骰子游戏

為{幸葍}努か 提交于 2019-12-06 02:28:51
import random print('*'*30+'欢迎进入大富翁游戏'+'*'*30) username = input('请输入用户名:') print('请充值购买金币后方可游戏,充值金额必须是100的整数倍') bit_conunt = 0 while True: my_money= int(input('请输入充值金额:')) if my_money > 0 and my_money % 100 == 0: bit_conunt = (my_money//100)*30 print('充值成功,您的充值金额是%d,账户金币为%d个。'%(my_money,bit_conunt),'请开始你的游戏!') break else: print('充值金额不符合规定,请输入正确金额进行充值。') while 1==1: big_small1 = random.randint(1,6) big_small2 = random.randint(1,6) guess_big_small = int(input('请输入你猜的数值(2-12之间):')) if big_small1 + big_small2 >= guess_big_small : bit_conunt = bit_conunt -2 print('你输了,您的剩余金币为:{}'.format(bit_conunt)

Java枚举类型(enum)-6

扶醉桌前 提交于 2019-12-06 02:25:04
EnumSet EnumSet是与枚举类型一起使用的专用 Set 集合,EnumSet 中所有元素都必须是枚举类型。与其他Set接口的实现类HashSet/TreeSet(内部都是用对应的HashMap/TreeMap实现的)不同的是,EnumSet在内部实现是位向量(稍后分析),它是一种极为高效的位运算操作,由于直接存储和操作都是bit,因此EnumSet空间和时间性能都十分可观,足以媲美传统上基于 int 的“位标志”的运算,重要的是我们可像操作set集合一般来操作位运算,这样使用代码更简单易懂同时又具备类型安全的优势。注意EnumSet不允许使用 null 元素。试图插入 null 元素将抛出 NullPointerException,但试图测试判断是否存在null 元素或移除 null 元素则不会抛出异常,与大多数collection 实现一样,EnumSet不是线程安全的,因此在多线程环境下应该注意数据同步问题,ok~,下面先来简单看看EnumSet的使用方式。 EnumSet用法 创建EnumSet并不能使用new关键字,因为它是个抽象类,而应该使用其提供的静态工厂方法,EnumSet的静态工厂方法比较多,如下: 创建一个具有指定元素类型的空EnumSet。 EnumSet<E> noneOf(Class<E> elementType) /

STM32基本GPIO操作:点灯(库函数+寄存器)

和自甴很熟 提交于 2019-12-05 22:14:19
社团作业=_= 开发版上的LED灯负极连接在PB5口,正极串联一510Ω电阻后与3.3V相连 若开发板不带LED灯则需要自行连接,务必串联一个合适的电阻防止LED灯烧坏 零、一个有趣的延时函数 来自于开发板配套资料当中的例程,第一次看到的时候觉得耳目一新,代码如下: void Delay(u32 count) { u32 i = 0; for (; i < count; i++) ; } 当中的u32类型是在stm32f10x.h当中的一个宏定义,对应uint32_t,表示32位无符号型整数,在我的开发板当中就是unsigned int类型。 因为STM32的主频比电脑CPU慢得多,因此可以通过这种循环的方式来达到延时的效果 一、库函数版本 1.初始化 以下是初始化PB5端口的代码 // 定义一个类型GPIO_InitTypeDef,名字叫做GPIO_InitStructure的结构体 GPIO_InitTypeDef GPIO_InitStructure; // PORTB时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 配置结构体GPIO_InitStructure GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // 设置GPIO端口号为5 GPIO

int32 storage in memory [duplicate]

徘徊边缘 提交于 2019-12-05 22:14:07
This question already has an answer here: How is an integer stored in memory? 4 answers I have a question about int32 storage (c#) . 32 bits means that the biggest number for int is 2^32. 2^32 = 4294967296 , if you divide it by 2 you get the maximum value for an int32 : 4294967296 / 2 = -2147483648 to 2147483648 So I thought half of the bits are for negative figures and the other for positive. But that cant be true because 2^16 = 65536 . Now my question : How is this actually set up in memory? Im really curious about your answers. Only one bit is used for sign (negative or positive) Int32

Java: Bitwise operation for flag checking

谁说胖子不能爱 提交于 2019-12-05 18:30:20
I'm trying to check whether or not a number has the second bit flag (ie 0000 0010). My code is as follows: int flags = Integer.parseInt(fields[1]); String strflags = Integer.toBinaryString(flags); flags = Integer.parseInt(strflags); int secondBitTest = Integer.parseInt("00000010", 2); if((flags & secondBitTest) == 2) { System.out.println("YES"); } However I think I might be doing this wrong, since when I try to input 147 nothing is returned. You can check if any bit is set using this code that I found here . if (x & (1<<n) != 0) { //n-th bit is set } else { //n-th bit is not set } x is the