bitwise-operators

MySQL: Update a SET field using named value and bit-operator

久未见 提交于 2020-06-28 10:20:19
问题 For MySQL, is it possible to update a field of type SET using one of the field's named values and bitwise operators? Example: Assume foo to be SET('a', 'b',...) , then the following does not work: UPDATE mytable SET foo = foo | 'a' WHERE ... Apparently, only foo = 'a' or foo = foo | 1 work. Is there any trick to get the above example working and make MySQL be aware of 'a' being not a "normal" string? I'd like to avoid magic numbers... Many thanks! 回答1: UPDATE mytable SET foo = CONCAT_WS(',',

variable expansion of ones in python list

拥有回忆 提交于 2020-06-28 05:17:22
问题 I have a python list of variable length filled with 0s and 1s . I want to create a new list where all 1s are expanded by a certain offset . Examples: offset = 1 l1 = [0,0,1,0] l1_new = l[0,1,1,1] l2 = [1,0,0,0,1,0,1,0,0] l2_new = [1,1,0,1,1,1,1,1,0] My solution code is not very fast and also does not use any numpy / vectorization / bitwise operations. But I guess some of those methods should be applicable here. offset = 1 l_old = [0,0,1,0] l_new = [] for i,l in enumerate(l_old): hit = False

variable expansion of ones in python list

烈酒焚心 提交于 2020-06-28 05:16:24
问题 I have a python list of variable length filled with 0s and 1s . I want to create a new list where all 1s are expanded by a certain offset . Examples: offset = 1 l1 = [0,0,1,0] l1_new = l[0,1,1,1] l2 = [1,0,0,0,1,0,1,0,0] l2_new = [1,1,0,1,1,1,1,1,0] My solution code is not very fast and also does not use any numpy / vectorization / bitwise operations. But I guess some of those methods should be applicable here. offset = 1 l_old = [0,0,1,0] l_new = [] for i,l in enumerate(l_old): hit = False

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

How do I use Java's bitwise operators in Kotlin?

霸气de小男生 提交于 2020-05-25 09:01:48
问题 Java has binary-or | and binary-and & operators: int a = 5 | 10; int b = 5 & 10; They do not seem to work in Kotlin: val a = 5 | 10; val b = 5 & 10; How do I use Java's bitwise operators in Kotlin? 回答1: You have named functions for them. Directly from Kotlin docs As of bitwise operations, there're no special characters for them, but just named functions that can be called in infix form. for example: val x = (1 shl 2) and 0x000FF000 Here is the complete list of bitwise operations (available

How do I use Java's bitwise operators in Kotlin?

百般思念 提交于 2020-05-25 08:59:50
问题 Java has binary-or | and binary-and & operators: int a = 5 | 10; int b = 5 & 10; They do not seem to work in Kotlin: val a = 5 | 10; val b = 5 & 10; How do I use Java's bitwise operators in Kotlin? 回答1: You have named functions for them. Directly from Kotlin docs As of bitwise operations, there're no special characters for them, but just named functions that can be called in infix form. for example: val x = (1 shl 2) and 0x000FF000 Here is the complete list of bitwise operations (available

In ASP, Bit Operator Left shift and Right shift

假如想象 提交于 2020-05-15 09:44:08
问题 Does anyone know left shift and right shift operator sample's? I'm new in ASP. I found Bit operators such as AND,OR,NOT only.. 回答1: For vbscript, left shift is accomplished by multiplication (i.e., var * 2 left shifts one position, var * 4 lefts shifts two positions, etc.) and right shift is accomplished by division (i.e., var \ 16 right shifts four positions). 回答2: There are no direct methods for left and right shift in vbscript, but as this is a simple move of each digit in a set of bits

Checksum and XOR in Swift

微笑、不失礼 提交于 2020-05-14 21:34:38
问题 I worte these methods in Objective-C. They're just checksum and XOR some NSData - (void)XOR:(NSMutableData *)inputData withKey:(NSData *)key { unsigned char* inputByteData = (unsigned char*)[inputData mutableBytes]; unsigned char* keyByteData = (unsigned char*)[key bytes]; for (int i = 0; i < [inputData length]; i++) { inputByteData[i] = inputByteData[i] ^ keyByteData[i % [key length]]; } } - (Byte)checkSum:(NSMutableData *)data withLength:(Byte)dataLength { Byte * dataByte = (Byte *)malloc

C: Sneaky way to count positive bits?

荒凉一梦 提交于 2020-05-12 15:59:33
问题 So I'm trying to see if there's some sneaky series of bit operations that will allow me to count how many bits in a uint32 are 1 (or rather the count mod 2). The "obvious" way would be something like this: uint32 count_1_bits_mod_2(uint32 word) { uint32 i, sum_mod_2; for(i = 0; i < 32; i++) sum_mod_2 ^= word; word >>= 1; Is there some "sneaky" way to get the proper sum_mod_2 without using a loop? 回答1: The fastest way to count bits is by using "magic numbers": unsigned int v = 0xCF31; // some

C: Sneaky way to count positive bits?

ぐ巨炮叔叔 提交于 2020-05-12 15:54:59
问题 So I'm trying to see if there's some sneaky series of bit operations that will allow me to count how many bits in a uint32 are 1 (or rather the count mod 2). The "obvious" way would be something like this: uint32 count_1_bits_mod_2(uint32 word) { uint32 i, sum_mod_2; for(i = 0; i < 32; i++) sum_mod_2 ^= word; word >>= 1; Is there some "sneaky" way to get the proper sum_mod_2 without using a loop? 回答1: The fastest way to count bits is by using "magic numbers": unsigned int v = 0xCF31; // some