bit-manipulation

The meaning of Bit-wise NOT in Python [duplicate]

限于喜欢 提交于 2019-12-10 15:53:51
问题 This question already has answers here : bit-wise operation unary ~ (invert) (5 answers) Closed last year . Why bitwise not does not act as expected for toggling bits? See for example below: a = 5 print(bin(a)) b = ~a print(bin(b)) This is the output: 0b101 -0b110 The question is why the first bit from the left is not toggled? Considering that Python documentation says: ~ x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. Edit: Are you saying

Is there a function to do circular bitshift for a byte array in C#?

白昼怎懂夜的黑 提交于 2019-12-10 15:48:52
问题 I can't seem to find if there's a built-in way to do a circular bitshift of a byte array, what C's ROL and ROR used to do with a single byte? Let me explain, say, I have an array (in binary): [0] = 11001110 [1] = 01000100 [2] = 10100001 and then if I want to do ROL_Array(1 bit) or move bits 1 bit to the left, I'd get: [0] = 10011100 [1] = 10001001 [2] = 01000011 or, if I want to do ROR_Array(2 bits) or move bits 2 bits to the right, I'd get: [0] = 00110011 [1] = 01010001 [2] = 10101000 回答1:

Is bitwise operation faster than modulo/reminder operator in Java?

独自空忆成欢 提交于 2019-12-10 15:45:06
问题 I read in couple of blogs that in Java modulo/reminder operator is slower than bitwise-AND. So, I wrote the following program to test. public class ModuloTest { public static void main(String[] args) { final int size = 1024; int index = 0; long start = System.nanoTime(); for(int i = 0; i < Integer.MAX_VALUE; i++) { getNextIndex(size, i); } long end = System.nanoTime(); System.out.println("Time taken by Modulo (%) operator --> " + (end - start) + "ns."); start = System.nanoTime(); final int

C Population Count of unsigned 64-bit integer with a maximum value of 15

回眸只為那壹抹淺笑 提交于 2019-12-10 15:36:50
问题 I use a population count (hamming weight) function intensively in a windows c application and have to optimize it as much as possible in order to boost performance. More than half the cases where I use the function I only need to know the value to a maximum of 15. The software will run on a wide range of processors, both old and new. I already make use of the POPCNT instruction when Intel's SSE4.2 or AMD's SSE4a is present, but would like to optimize the software implementation (used as a

Bit manipulation in C# using a mask

我的未来我决定 提交于 2019-12-10 15:16:59
问题 I need a little help with bitmap operations in C# I want to take a UInt16 , isolate an arbitrary number of bits, and set them using another UInt16 value. Example: 10101010 -- Original Value 00001100 -- Mask - Isolates bits 2 and 3 Input Output 00000000 -- 10100010 00000100 -- 10100110 00001000 -- 10101010 00001100 -- 10101110 ^^ 回答1: It seems like you want: (orig & ~mask) | (input & mask) The first half zeroes the bits of orig which are in mask . Then you do a bitwise OR against the bits from

NASM shift operators

 ̄綄美尐妖づ 提交于 2019-12-10 15:14:55
问题 How would you go about doing a bit shift in NASM on a register? I read the manual and it only seems to mention these operators >> , << . When I try to use them NASM complains about the shift operator working on scalar values. Can you explain what a scalar value is and give an example of how to use >> and << . Also, I thought there were a shr or shl operators. If they do exist can you give an example of how to use them? Thank you for your time. 回答1: << and >> are for use with integer constants

C - Using bit-shift operators for base conversion

不问归期 提交于 2019-12-10 14:55:07
问题 I'm trying to convert some data from hex to base64 in C, I found an algorithm online but I would really like to know how it works rather than just implenting it and firing it off. If someone could please explain how the following is working I would appreciate it. I have been reading about the shift operators and I don't seem to understand them as much as I thought I did...it's not quite clicking for me. for (x = 0; x < dataLength; x += 3) { /* these three 8-bit (ASCII) characters become one

Bitwise operation in C to compare two integers

♀尐吖头ヾ 提交于 2019-12-10 14:51:37
问题 I was recently given a quiz in one of my classes. The question is below: Write a function (called cmp ) in C that accepts two integers ( x and y ) and returns: -1 if x < y , 0 if x = y , 1 if x > y . Write cmp as concise as possible. The most concise function that I could think of was: int cmp(int x, int y) { return ((x < y) ? (-1) : ((x == y) ? (0) : (1))); } But I have a feeling there could be a bit manipulation that I could use to do this more concisely. Perhaps a combination of & and ^ ?

Long type 64bit linux

a 夏天 提交于 2019-12-10 14:47:56
问题 Very simple questions guys, but maybe I'm just forgetting something. In 64bit linux, a long is 8bytes correct? If that's the case, and I want to set the 64th bit, I can do the following: unsigned long num = 1<<63; Whenever I compile this, however, it gives me an error saying that I'm left shifting by more than the width. Also, if I wanted to take the first 32bits of a long type (without sign extension), can I do: num = num&0xFFFFFFFF; or what about: num = (int)(num); Thank you. 回答1: In 64bit

How to turn a division into a bitwise shift when power of two?

夙愿已清 提交于 2019-12-10 14:33:17
问题 I have the following division that I need to do often: int index = pos / 64; Division can be expensive in the cpu level. I am hoping there is a way to do that with bitwise shift. I would also like to understand how you can go from division to shift, in other words, I don't want to just memorize the bitwise expression. 回答1: int index = pos >> 6 will do it, but this is unnecessary. Any reasonable compiler will do this sort of thing for you. Certainly the Sun/Oracle compiler will. The general