bit-manipulation

Bit manipulation in negative byte and short data types in Java

时光怂恿深爱的人放手 提交于 2020-01-16 20:21:48
问题 Im trying to implement a class that stores a 32-bit number without using the int primitive type. For doing so, I'm using two short variables msbs and lsbs to store the 32 bits of the number, 16 bits in each variable. The variable msbs will store the first 16 bits of the number and the lsbs variable the 16 bits left. When It comes to save the given bytes to the variables I apply the next formula: (The bytes order are given as Little-Endian notation) Input -> byte[] n = {0b00110101, -3, 0b1001,

Bit manipulation in negative byte and short data types in Java

别等时光非礼了梦想. 提交于 2020-01-16 20:21:21
问题 Im trying to implement a class that stores a 32-bit number without using the int primitive type. For doing so, I'm using two short variables msbs and lsbs to store the 32 bits of the number, 16 bits in each variable. The variable msbs will store the first 16 bits of the number and the lsbs variable the 16 bits left. When It comes to save the given bytes to the variables I apply the next formula: (The bytes order are given as Little-Endian notation) Input -> byte[] n = {0b00110101, -3, 0b1001,

masking most significant bit

拥有回忆 提交于 2020-01-16 10:38:06
问题 I wrote this function to remove the most significant bit in every byte. But this function doesn't seem to be working the way I wanted it to be. The output file size is always '0', I don't understand why nothing's been written to the output file. Is there a better and simple way to remove the most significant bit in every byte?? 回答1: In relation to shift operators, section 6.5.7 of the C standard says: If the value of the right operand is negative or is greater than or equal to the width of

masking most significant bit

筅森魡賤 提交于 2020-01-16 10:38:02
问题 I wrote this function to remove the most significant bit in every byte. But this function doesn't seem to be working the way I wanted it to be. The output file size is always '0', I don't understand why nothing's been written to the output file. Is there a better and simple way to remove the most significant bit in every byte?? 回答1: In relation to shift operators, section 6.5.7 of the C standard says: If the value of the right operand is negative or is greater than or equal to the width of

Divide two integers using only bitwise operations [duplicate]

风格不统一 提交于 2020-01-16 08:24:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: implement division with bit wise operator I recently got into more depth by bitwise functions, and started to implement basic arithmetic functions with bitwise operators. So far I have gotten (+, -, *) . However I'm not really sure how to approach division. I know that I could somehow use multiplication instead, but not sure how to approach this using that method either. So how would I implement division using

Is -!(condition) a correct way to obtain a full-bitvector from a boolean (mask-boolean)?

前提是你 提交于 2020-01-16 04:12:45
问题 In removing conditional branches from high-performance code, converting a true boolean to unsigned long i = -1 to set all bits can be useful. I came up with a way to obtain this integer-mask-boolean from input of a int b (or bool b ) taking values either 1 or 0 : unsigned long boolean_mask = -(!b); To get the opposite value: unsigned long boolean_mask = -b; Has anybody seen this construction before? Am I on to something? When a int value of -1 (which I assume -b or -(!b) does produce) is

How to bitwise shift a binary string in java?

只谈情不闲聊 提交于 2020-01-15 10:39:09
问题 I have a 10 bit binary string and i have to bitwise shift circularly at each iteration. I am so confused and lost doing it. What could be the logic behind it to do easily? If it is a hex number, we can do it by num>>1 or num<<1 but string like "1010101010" should be converted to hex before we apply bitwise shift. I have to apply the bitwise shift circularly 10 times. 回答1: There are methods in the Integer class to convert to/from Binary Strings. int i = Integer.parseInt("1010101010", 2);

Find the lowest set bit

梦想的初衷 提交于 2020-01-15 10:18:07
问题 I have 5 bit numbers like 10000 01000 00100 If only one bit is on in my calculation i have no problem. but if 2 bits are on then I want to select only the first on bit for example 10010 i want to treat it as 2 instead of the number 18 is there any bitwise operation which may i use in such sitution? 回答1: Since you only want to isolate it, not get its index, it's easy: function firstSetBit(number) { return number & -number; } It works because if you take the two's complement negation of a

How to find index of first set bit

对着背影说爱祢 提交于 2020-01-14 14:45:28
问题 Is there bitwise solution to find the index of first set bit in mask with only one bit set? e.g. for 8 it would be 3, for 16 => 4 and so on. No loops plz. The best solution I can come up with is to createa map of bit to index. 回答1: function firstBit(x) { return Math.floor( Math.log(x | 0) / Math.log(2) ) + 1; } i=4; console.log(i.toString(2), firstBit(i)); // 100 3 i=7; console.log(i.toString(2), firstBit(i)); // 111 3 i=8; console.log(i.toString(2), firstBit(i)); // 1000 4 回答2: For posterity

Finding position of '1's efficiently in an bit array

十年热恋 提交于 2020-01-14 09:48:27
问题 I'm wiring a program that tests a set of wires for open or short circuits. The program, which runs on an AVR, drives a test vector (a walking '1') onto the wires and receives the result back. It compares this resultant vector with the expected data which is already stored on an SD Card or external EEPROM. Here's an example, assume we have a set of 8 wires all of which are straight through i.e. they have no junctions. So if we drive 0b00000010 we should receive 0b00000010. Suppose we receive