bit-manipulation

Bitwise shift operation in C on uint64_t variable

北战南征 提交于 2019-12-01 22:08:21
I have the following sample code: uint64_t x, y; x = ~(0xF<<24); y = ~(0xFF<<24); The result would be: x=0xfffffffff0ffffff y=0xfffff Can anyone explain the difference? Why x is calculated over 64 bit and y only on 32? The default operation is 32 bit. x=~(0xf<<24); This code could be disassembled into the following steps: int32_t a; a=0x0000000f; a<<=24; // a=0x0f000000; a=~a; // a=0xf0ffffff; x=(uint64_t)a; // x = 0xfffffffff0ffffff; And, y = ~(0xFF<<24); int32_t a; a=0x000000ff; a<<=24; // a=0xff000000; a=~a; // a=0x00ffffff; x=(uint64_t)a; // x = 0x000000000ffffff; Because 0x0f << 24 is a

JavaScript Bitwise Masking

北慕城南 提交于 2019-12-01 22:04:44
This question is similar to this other question ; however, I'd like to understand why this is working as it is. The following code: console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) &

Reading least significant bits in Python

半腔热情 提交于 2019-12-01 22:00:37
I am having to parse the Facility and Severity of syslog messages in Python. These values come with each message as a single integer. The severity of the event is 0-7, specified in the 3 least significant bits in the integer. What is the easiest/fastest way to evaluate these 3 bits from the number? The code I have right now just does a 3 bit right shift, than multiplies that number times 8, and subtracts the result from the original. FAC = (int(PRI) >> 3) SEV = PRI - (FAC * 8) There must be a less convoluted way to do this- rather than wiping out the bits, and subtracting. SEV = PRI & 7 FAC =

Bitwise shift operation in C on uint64_t variable

元气小坏坏 提交于 2019-12-01 21:53:11
问题 I have the following sample code: uint64_t x, y; x = ~(0xF<<24); y = ~(0xFF<<24); The result would be: x=0xfffffffff0ffffff y=0xfffff Can anyone explain the difference? Why x is calculated over 64 bit and y only on 32? 回答1: The default operation is 32 bit. x=~(0xf<<24); This code could be disassembled into the following steps: int32_t a; a=0x0000000f; a<<=24; // a=0x0f000000; a=~a; // a=0xf0ffffff; x=(uint64_t)a; // x = 0xfffffffff0ffffff; And, y = ~(0xFF<<24); int32_t a; a=0x000000ff; a<<=24

What's the best way to toggle the MSB?

佐手、 提交于 2019-12-01 21:38:12
So I want to toggle the most significant bit of my number. Here is an example: x = 100101 then answer should be 00101 I have a 64 bit machine and hence I am not expecting the answer to be 100000..<51 0's>..100101 One way I thought of was to count the number of bits in my number and then toggle the MSB, but not sure on how to count. jleahy The cheat is to pawn it off to the compiler: There are instructions in most CPUs for doing work like this. The following should do what you want. i ^ (1 << (sizeof i * CHAR_BIT - clz(i) - 1)) This will translate into the CLZ instruction, which counts the

set individual bit in AVX register (__m256i), need “random access” operator

微笑、不失礼 提交于 2019-12-01 21:19:56
So, I want to set an individual bit of a __m256i register. Say, my __m256i contains: [ 1 0 1 0 | 1 0 1 0 | ... | 1 0 1 0 ] , how do I set and unset the n-th bit? ErmIg This is an implementation of function which can set individual bit inside a vector: #include <immintrin.h> #include <assert.h> void SetBit(__m256i & vector, size_t position, bool value) { assert(position <= 255); uint8_t lut[32] = { 0 }; lut[position >> 3] = 1 << (position & 7); __m256i mask = _mm256_loadu_si256((__m256i*)lut); if (value) vector = _mm256_or_si256(mask, vector); else vector = _mm256_andnot_si256(mask, vector); }

Generate matrix of bits

为君一笑 提交于 2019-12-01 21:16:20
I would like to take an integer n defining the number of bits in my communication code and a vector defining the alphabet I am assigning to bits 0:n-1 , and output a matrix/cell array containing the alphabetic notation for each state, i.e.: function M = mycommarray(3,[-1,1]) produces M = [{-1,-1,-1}, {-1,-1,1}...] I tried doing this an easier way with dec2bin(0:7,3) , but there doesn't seem to be a quick way to make the zeros into -1 s. Is there anything close to prepackaged that does this? In this case I don't want anyone to make it for me (related to homework). dec2bin is actually not the

Simulating Floating Point Multiplication in C using Bitwise Operators [closed]

北慕城南 提交于 2019-12-01 21:13:29
I have to write a program that will simulate floating point multiplication. For this program, we assume that a single precision floating point number is stored in unsigned long a . I have to multiply the number stored in a by 2 using only the following operators: << >> | & ~ ^ I understand the functions of these operators, but I'm confused on the logic of how to go about implementing this. Any help would be greatly appreciated. have to multiply the number stored in a by 2 using only the following operators: << >> | & ~ ^ since we are given an unsigned long to simulate a float value with a

regarding left shift and right shift operator

萝らか妹 提交于 2019-12-01 21:10:35
void times(unsigned short int time) { hours=time>>11; minutes=((time<<5)>>10); } Take the input time to be 24446 The output values are hours = 11 minutes = 763 The expected values are hours = 11 minutes = 59 What internal processing is going on in this code? Binary of 24446 is 0101111101111110 Time>>11 gives 01011 which means 11 . ((Time<<5)>>10) gives 111011 which means 59 . But what else is happening here? What else is going on here? If time is unsigned short , there is an important difference between minutes=((time<<5)>>10); and unsigned short temp = time << 5; minutes = temp >> 10; In both

Bitwise Less than or Equal to

我的未来我决定 提交于 2019-12-01 21:07:38
问题 There seems to be some kind of misconception that this is for a contest. I'm trying to work through an assignment and I've been stuck on this for an hour now. /* * isLessOrEqual - if x <= y then return 1, else return 0 * Example: isLessOrEqual(4,5) = 1. * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */ int isLessOrEqual(int x, int y) { int greater = (x + (~y + 1))>>31 & 1; return !(greater)|(!(x^y)); } I'm only able to use bitwise operators, as instructed in the comments. I cannot