bit-manipulation

Golang bitwise operations as well as general byte manipulation

只愿长相守 提交于 2019-12-06 03:38:55
问题 I have some c# code that performs some bitwise operations on a byte. I am trying to do the same in golang but am having difficulties. Example in c# byte a, c; byte[] data; int j; c = data[j]; c = (byte)(c + j); c ^= a; c ^= 0xFF; c += 0x48; I have read that golang cannot perform bitwise operations on the byte type. Therefore will I have to modify my code to a type uint8 to perform these operations? If so is there a clean and correct/standard way to implement this? 回答1: Go certainly can do

QWORD shuffle sequential 7-bits to byte-alignment with SIMD SSE…AVX

泪湿孤枕 提交于 2019-12-06 02:09:45
I would like to know if the following is possible in any of the SIMD families of instructions. I have a qword input with 63 significant bits (never negative). Each sequential 7 bits starting from the LSB is shuffle-aligned to a byte, with a left-padding of 1 (except for the most significant non-zero byte). To illustrate, I'll use letters for clarity's sake. The result is only the significant bytes, thus 0 - 9 in size, which is converted to a byte array. In: 0|kjihgfe|dcbaZYX|WVUTSRQ|PONMLKJ|IHGFEDC|BAzyxwv|utsrqpo|nmlkjih|gfedcba Out: 0kjihgfe|1dcbaZYX|1WVUTSRQ|1PONMLKJ|1IHGFEDC|1BAzyxwv

Bitwise Float To Int

…衆ロ難τιáo~ 提交于 2019-12-06 01:45:01
I am trying to figure out the algorithm to this but all I get with google is doing it with casting. I need to know the details. So if we have a float x and want to return its binary representation what do we need to do? I know we need to return the float if its NaN or a infinity but otherwise what are the steps? EDIT The function takes in an unsigned int, to be used as if it was a float, and then return the integer the number represents. I cannot use casting, just conditionals and bit-wise operators. Alternatively, use a union: typedef union { float f_; int i_; } FloatBits; FloatBits fb; fb.f_

Number of unset bit left of most significant set bit?

*爱你&永不变心* 提交于 2019-12-05 23:33:27
问题 Assuming the 64bit integer 0x000000000000FFFF which would be represented as 00000000 00000000 00000000 00000000 00000000 00000000 >11111111 11111111 How do I find the amount of unset bits to the left of the most significant set bit (the one marked with >) ? 回答1: // clear all bits except the lowest set bit x &= -x; // if x==0, add 0, otherwise add x - 1. // This sets all bits below the one set above to 1. x+= (-(x==0))&(x - 1); return 64 - count_bits_set(x); Where count_bits_set is the fastest

Reverse bytes order of long

六眼飞鱼酱① 提交于 2019-12-05 21:35:11
I've got a long variable and I need to reverse its byte order. For example: B1, B2, ... , B8 I should return a long that consists of B8, B7, ..., B1 . How can I do it by using bitwise operations? Serdalis you can use Long.reverseBytes(long) Or for more methods which include bitwise operations, you can refer to this stack overflow question Heres another method you may like, I'd still recommend the above but it's better than bitwise where you can easily make mistakes. Bytebuffer byte[] bytes = ByteBuffer.allocate(8).putLong(someLong).array(); for (int left = 0, right = bytes.length - 1; left <

Applications of a circular shift

心已入冬 提交于 2019-12-05 21:03:00
I would like to know some examples of application of circular shifts. For example, a right shift on an unsigned integer would result in a division by two. Conversely, a left shift would result in a multiplication by 2. Are there any famous/interesting properties of a circular shift on binary numbers. Note: The example about the right/left shift is to illustrate an application of that particular operator. I am asking for similar examples for the circular shift operator/function. Convert a 16-bit word between big-endian and little-endian representation: right or left circular shift by 8.

Are bitwise operations going to help me to serialize some bools?

别来无恙 提交于 2019-12-05 20:56:36
I'm not used to binary files, and I'm trying to get the hang of it. I managed to store some integers and unsigned char, and read them without too much pain. Now, when I'm trying to save some booleans, I see that each of my bool takes exactly 1 octet in my file, which seems logical since a lone bool is stored in a char-sized data (correct me if I'm wrong!). But since I'm going to have 3 or 4 bools to serialize, I figure it is a waste to store them like this : 00000001 00000001 00000000, for instance, when I could have 00000110. I guess to obtain this I should use bitwise operation, but I'm not

Branching elimination using bitwise operators

不想你离开。 提交于 2019-12-05 20:33:34
问题 I have some critical branching code inside a loop that's run about 2^26 times. Branch prediction is not optimal because m is random. How would I remove the branching, possibly using bitwise operators? bool m; unsigned int a; const unsigned int k = ...; // k >= 7 if(a == 0) a = (m ? (a+1) : (k)); else if(a == k) a = (m ? 0 : (a-1)); else a = (m ? (a+1) : (a-1)); And here is the relevant assembly generated by gcc -O3 : .cfi_startproc movl 4(%esp), %edx movb 8(%esp), %cl movl (%edx), %eax testl

Setting a bit of an unsigned char with the another bit of another unsigned char without conditional

时光总嘲笑我的痴心妄想 提交于 2019-12-05 20:10:19
I use bitwise to turn bits on and off this way: unsigned char myChar = ...some value myChar |= 0x01 << N // turn on the N-th bit myChar &= ~(0x01 << N) //turn off the N-th bit Now, suppose the value of N is know but the set/unset operation depends on the value of a bit of another unsigned char. Since now, I'm doing this way: if ((otherChar & (0x01 << M)) != 0) { //M-th bit of otherChar is 1 myChar |= 0x01 << N; }else { myChar &= ~(0x01 << N); } This should be a sort of "moving bit" operation from an unsigned char to another. My question: is there any way of doing this without using the

Bit masking in Python

筅森魡賤 提交于 2019-12-05 20:06:02
问题 I have a byte (from some other vendor) where the potential bit masks are as follows: value1 = 0x01 value2 = 0x02 value3 = 0x03 value4 = 0x04 value5 = 0x05 value6 = 0x06 value7 = 0x40 value8 = 0x80 I can count on ONE of value1 through value6 being present. And then value7 may or may not be set. value8 may or may not be set. So this is legal: value2 | value7 | value8 This is not legal: value1 | value3 | value7 I need to figure out whether value 7 is set, value8 is set, and what the remaining