bit-manipulation

bitwise OR (on array)

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:02:46
问题 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 */ 回答1: 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; } 回答2: I think your best bet is to use a BitSet .

Is there an elegant way to Invert a Bit value in an SQL insert Statement?

落爺英雄遲暮 提交于 2019-12-03 00:53:56
I'm converting some data in SQL Server: INSERT INTO MYTABLE (AllowEdit) (Select PreventEdit from SOURCETABLE) so I need to inverse the bit value from source table. I expected NOT to work, as this is how I would do it in code, but it doesn't. The most elegant way I can think of is: INSERT INTO MYTABLE (AllowEdit) (Select ABS(PreventEdit -1) from SOURCETABLE) Is there a more standard way to do it? I did not test this myself, but you should be able to use the bitwise negation operator , ~ on a bit: INSERT INTO MYTABLE (AllowEdit) (SELECT ~PreventEdit FROM SourceTable) NOT or XOR if bit SELECT

How can I turn an int into three bytes in Java?

心已入冬 提交于 2019-12-03 00:50:04
I am trying to convert an int into three bytes representing that int (big endian). I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it. For example: int myInt; // some code byte b1, b2 , b3; // b1 is most significant, then b2 then b3. *Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing. To get the least significant byte: b3 = myInt & 0xFF; The 2nd least significant byte: b2 = (myInt >> 8) & 0xFF; And the 3rd least significant byte: b1 = (myInt >> 16) & 0xFF; Explanation: Bitwise ANDing a

Conditional Statement using Bitwise operators

邮差的信 提交于 2019-12-02 23:53:55
So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only "& ^ ~ ! + | >> <<" The expression needs to resemble: a ? b : c So, from what I've been able to tell, the expression needs to look something like: return (a & b) | (~a & c) This works when a = 0, because anding it with b will give zero, and then the or expression will return the right side, (~a & c) which works because ~0 gives all ones, and anding c with all ones returns c. However, this doesn't work when a > 0. Can someone try to

algorithm behind the generation of the reverse bits lookup table(8 bit)

*爱你&永不变心* 提交于 2019-12-02 23:53:11
I found the lookup table here. The table is generated as a reverse bits table of 8 bits. I can not figure out why it works. Please explain the theory behind it. Thanks static const unsigned char BitReverseTable256[256] = { # define R2(n) n, n + 2*64, n + 1*64, n + 3*64 # define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16) # define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 ) R6(0), R6(2), R6(1), R6(3) }; First off a comment: This kind of thing is normally only done in the IOCCC . Code like this should not be used in production-environments because it is non-obvious . The

How do bitwise operator results occur?

跟風遠走 提交于 2019-12-02 23:35:25
问题 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

How in swift to convert Int16 to two UInt8 Bytes

十年热恋 提交于 2019-12-02 23:20:32
I have some binary data that encodes a two byte value as a signed integer. bytes[1] = 255 // 0xFF bytes[2] = 251 // 0xF1 Decoding This is fairly easy - I can extract an Int16 value from these bytes with: Int16(bytes[1]) << 8 | Int16(bytes[2]) Encoding This is where I'm running into issues. Most of my data spec called for UInt and that is easy but I'm having trouble extracting the two bytes that make up an Int16 let nv : Int16 = -15 UInt8(nv >> 8) // fail UInt8(nv) // fail Question How would I extract the two bytes that make up an Int16 value You should work with unsigned integers: let bytes:

negate floating number in c fails in some cases

梦想与她 提交于 2019-12-02 22:57:40
问题 I wrote a function which works for hundreds of cases but fails in some cases. Here is the C function: unsigned negate_number(unsigned x) { int sign = (!(x & 0x80000000))<<31; int other = 0x7FFFFFFF & x; return (sign | other); } I am just masking sign, inverting it and doing a OR (joining) with masked exponent and mantessa. So this should work in all cases. But here is a case where it fails: x = 0x7fc00000 (2143289344) 回答1: I asked: Why are you asking about 'floating number' in the title when

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

。_饼干妹妹 提交于 2019-12-02 22:47:15
问题 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

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

冷暖自知 提交于 2019-12-02 22:45:12
问题 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.