bit-manipulation

Convert MBF Double to IEEE

拈花ヽ惹草 提交于 2019-12-02 04:57:28
I found a topic below for convert MBF to IEEE. Convert MBF Single and Double to IEEE Anyone can explain what are the function of the code marked below? Dim sign As Byte = mbf(6) And ToByte(&H80) 'What is the reason AND (&H80)? Dim exp As Int16 = mbf(7) - 128S - 1S + 1023S 'Why is 1152 (128+1+1023)? ieee(7) = ieee(7) Or sign 'Why don't just save sign to ieee(7)? ieee(7) = ieee(7) Or ToByte(exp >> 4 And &HFF) 'What is the reason to shift 4? Public Shared Function MTID(ByVal src() As Byte, ByVal startIndex As Integer) As Double Dim mbf(7) As Byte Dim ieee(7) As Byte Array.Copy(src, startIndex,

How to apply bitwise operations to the actual IEEE 754 representation of JS Numbers?

对着背影说爱祢 提交于 2019-12-02 04:29:37
In JavaScript, whenever you perform a bitwise operation such as x << 2 , the 64-bit float representation gets converted to a 32-bit unsigned int before the shifting actually occurs. I am insterested in applying the shift to the actual, unaltered IEEE 754 bitwise representation. How is that possible? You might try converting the JSNumber to bytes/integers first and shifting the result yourself. Using TypedArray stuff available in recent versions of major browsers: var f = new Float64Array( 1 ); // creating typed array to contain single 64-bit IEEE754 f.set( [ 1.0 ], 0 ); // transferring

Unexepected behavior from multiple bitwise shifts on the same line [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-02 04:28:44
问题 This question already has answers here : Why does combining two shifts of a uint8_t produce a different result? (2 answers) Closed 5 years ago . I get a different result depending on if I combine multiple bitwise shifts on one line or put them on separate lines. unsigned char a = 73; a = (a << 6) >> 2; printf("%d\n", a); prints 144 when I expect 16 unsigned char a = 73; a = a << 6; a = a >> 2; printf("%d\n", a); prints the expected result of 16 when I shift left 6, then right 1 on one line,

Toggle a Specific Bit

徘徊边缘 提交于 2019-12-02 04:27:14
问题 So I have seen the questions like toggle a bit at ith positon and How do you set, clear, and toggle a single bit?, but I was wondering if there was a good way to toggle a bit in the ith position in x86-64 assembly? I tried writing it in C and looking through the assembly and don't quite understand exactly why there are some things that are there. C: unsigned long toggle(unsigned long num, unsigned long bit) { num ^= 1 << bit; return num; } int main() { printf("%ld\n", toggle(100, 60)); return

php bitwise XOR and js bitwise XOR producing different results

大城市里の小女人 提交于 2019-12-02 04:22:57
When I try to perform bitwise XOR operation in php and js, they are producing different results in some cases , for example 2166136261 ^ 101 = -2128831072 on browsers (js) 2166136261 ^ 101 = 2166136224(php) My understanding is because php is running 64 bit as opposed to 32 bit js. Can anyone tell me the exact reason and if this could be solved so that both operations result in same value. Thanks! 2,147,483,647 is the biggest possible positive value for an integer in 32 bit computing, (it's 2^16, half of the 32 bits we have, the other half are reserved for negative numbers.) Once you use a

Fast way to remove bits from a ulong

时光毁灭记忆、已成空白 提交于 2019-12-02 03:59:48
问题 I want to remove bits from a 64 bit string (represented by a unsigned long). I could do this with a sequence of mask and shift operations, or iterate over each bit as in the code below. Is there some clever bit-twiddling method to make this perform quicker? public ulong RemoveBits(ulong input, ulong mask) { ulong result = 0; ulong readbit = 1; ulong writebit =1; for (int i = 0; i < 64; i++) { if ((mask & readbit) == 0) //0 in the mask means retain that bit { if ((input & readbit) > 0) {

Problem with Bitwise Barrel Shift Rotate left and right in C#

心已入冬 提交于 2019-12-02 03:53:25
In C++ I have code like this. static UInt32 rol(UInt32 value, UInt32 bits) { bits &= 31; return ((value << bits) | (value >> (32 - bits))); } static UInt32 ror(UInt32 value, UInt32 bits) { bits &= 31; return ((value >> bits) | (value << (32 - bits))); } how would it look in C#? I think the same exact way.. only problem Error 2 Operator '>>' cannot be applied to operands of type 'uint' and 'uint' Error 3 Operator '>>' cannot be applied to operands of type 'uint' and 'uint' Error 1 Operator '<<' cannot be applied to operands of type 'uint' and 'uint' Error 4 Operator '<<' cannot be applied to

Bitwise concatenation in C

佐手、 提交于 2019-12-02 03:44:26
问题 I'm trying to concatenate two binary numbers in C. So if I have 1010 and 0011 I want my result to be 10100011 . I wrote a short routine that I thought would do the job: #include <stdio.h> int main(void) { int first = 1010; int second = 0011; int result = (first << 4) | second; printf("%d", result); return 0; } I understand that the printed number of course is going to be in decimal, but I figured that after my bitwise operations I'd be getting the decimal equivalent of 10100011 , or 163.

Efficient random permutation of n-set-bits

帅比萌擦擦* 提交于 2019-12-02 03:41:33
问题 For the problem of producing a bit-pattern with exactly n set bits, I know of two practical methods, but they both have limitations I'm not happy with. First, you can enumerate all of the possible word values which have that many bits set in a pre-computed table, and then generate a random index into that table to pick out a possible result. This has the problem that as the output size grows the list of candidate outputs eventually becomes impractically large. Alternatively, you can pick n

What is this operator: &=

▼魔方 西西 提交于 2019-12-02 03:40:24
Can someone explain what is the operator &= for? I searched, but I got only results with & or = . a &= b; Is the same as a = a & b; & is the "bitwise and operator", search for that. It's a shorthand operator which allows you to collapse a = a & b into a &= b Apart from bitwise operations on integers, &= can be used on boolean values as well, allowing you to collapse a = a && b into a &= b However, in the case of logical operation, the expanded form is short-circuiting, while the latter collapsed form does not short-circuit. Example: let b() be a function that returns a value and also does