bit-manipulation

why shift int a=1 to left 31 bits then to right 31 bits, it becomes -1

假装没事ソ 提交于 2019-12-02 13:59:28
given int a = 1; ( 00000000000000000000000000000001 ), what I did is just a=(a<<31)>>31; I assume a should still be 1 after this statement (nothing changed I think). However, it turns out to be -1 ( 11111111111111111111111111111111 ). Anyone knows why? What you are missing is that in C++ right shift >> is implementation defined. It could either be logical or arithmetic shift for a signed value. In this case it's shifting in 1 s from the left to retain the sign of the shifted value. Typically you want to avoid doing shifts on signed values unless you know precisely that they will be positive or

Error when trying to perform a bitwise not (~) on a UInt16 in C#

泪湿孤枕 提交于 2019-12-02 13:44:27
For some reason, I am simply not understanding (or seeing) why this works: UInt32 a = 0x000000FF; a &= ~(UInt32)0x00000001; but this does not: UInt16 a = 0x00FF; a &= ~(UInt16)0x0001; it gives the error 'constant value -(some number) cannot be converted to a UInt16'. Can someone explain why this is and how to work around the problem? The bitwise negation promotes the result to a int , despite the cast. You can overcome this by bitwise and-ing the result of the bitwise negation to the lower 16 bits, like this: ~0x0001 & 0xFFFF . Because §14.6.4 of the Standard (ISO 23270) says: 14.6.4 Bitwise

bitwise OR (on array)

冷暖自知 提交于 2019-12-02 13:26:15
I need to perform bitwise OR of two arrays of byte in Java. How can I do so? byte a= new byte[256]; byte b= new byte[256]; byte c; /*it should contain information i.e bitwise OR of a and b */ Thats as simple as using the | operator and a loop: public static byte[] byteOr(byte[] a, byte[] b) { int len = Math.min(a.length, b.length); byte[] result = new byte[len]; for (int i=0; i<len; ++i) result[i] = (byte) (a[i] | b[i]) return result; } I think your best bet is to use a BitSet . That class already has a void or(BitSet bs) method to use. byte a = new byte[256]; byte b = new byte[256]; byte c =

php bitwise XOR and js bitwise XOR producing different results

扶醉桌前 提交于 2019-12-02 13:21:38
问题 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! 回答1: 2,147,483,647 is the biggest possible positive value for an integer in 32 bit computing, (it

How can I find how many times does 101b show in the number?

荒凉一梦 提交于 2019-12-02 13:10:39
I need to find out how many times does the sequence 101b shows in a 16 bit number. But I need to find also the ones that are far away. For example: in the number 01010101 it appears 4 times. Because 3 are part of it and the fourth one (if index 0 is the left bit) is composed of the 3 bits at index 1, 4, and 7. Because you can relate to it as symmetrical 101b sequence. It seems really complicated, but does it really? I think it might just be a little tricky. I managed to find out how many times it shows regularly, like the 3 that you can see in the example number. But I don't know how can I

Finding how many bits it takes to represent a 2's complement using only bitwise functions

情到浓时终转凉″ 提交于 2019-12-02 13:03:41
We can assume an int is 32 bits in 2's compliment The only Legal operators are: ! ~ & ^ | + << >> At this point i am using brute force int a=0x01; x=(x+1)>>1; //(have tried with just x instead of x+1 as well) a = a+(!(!x)); ... with the last 2 statements repeated 32 times. This adds 1 to a everytime x is shifted one place and != 0 for all 32 bits Using the test compiler it says my method fails on test case 0x7FFFFFFF (a 0 followed by 31 1's) and says this number requires 32 bits to represent. I dont see why this isnt 31 (which my method computes) Can anyone explain why? And what i need to

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

主宰稳场 提交于 2019-12-02 12:16:30
问题 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? 回答1: 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

How to bitwise-and CFBitVector

谁说胖子不能爱 提交于 2019-12-02 12:07:06
I have two instances of CFMutableBitVector , like so: CFBitVectorRef ref1, ref2; How can I do bit-wise operations to these guys? For right now, I only care about and , but obviously xor , or , etc would be useful to know. Obviously I can iterate through the bits in the vector, but that seems silly when I'm working at the bit level. I feel like there are just some Core Foundation functions that I'm missing, but I can't find them. Thanks, Kurt Well a CFBitVectorRef is a typedef const struct __CFBitVector *CFBitVectorRef; which is a struct __CFBitVector { CFRuntimeBase _base; CFIndex _count; /*

I need help on Bit Twiddling

蓝咒 提交于 2019-12-02 11:11:07
问题 I love to see people writing Bit Twiddling code but I just can't understand it at all. Gone through Hacker's Delight and http://graphics.stanford.edu/~seander/bithacks.html, but I failed to understand anything. For example: How come 1 | 2 returns 3 or how come a ^=b; b ^= a; a ^=b; swap the values etc... One method: private T[] ensureCapacity(int minCapacity) { if (tmp.length < minCapacity) { // Compute smallest power of 2 > minCapacity newSize |= newSize >> 1; int newSize = minCapacity;

How do bitwise operator results occur?

非 Y 不嫁゛ 提交于 2019-12-02 10:52:57
I'm quite surprised that I can't find an answer to this simple sounding question on Google. After checking about a dozen different pages I'm just going to ask here ... According to this page , 3 & 5 result in 1. Also, 3 | 5 result in 7. The only question I have is simply: How do we get 1 for 3 & 5? How to we get 7 for 3 | 5? Also, what about negative numbers? How does 8 & -8 result in 8? Sure enough, writing the following in java: System.out.println(3&5); System.out.println(3|5); System.out.println(8&-8); Produces this output: 1 7 8 But again, how are these results determined / calculated?