bit-manipulation

Using bitwise & operator and + in Java giving inconsistent results

耗尽温柔 提交于 2019-12-01 16:56:16
问题 Could someone please explain why these two pieces of Java codes are behaving differently? First one correctly counts number of bits but the second one just displays 1 or 0 for non-zero numbers. I don't understand whats happening. public static void printNumUnitBits(int n){ int num=0; for(int i=0;i<32;i++){ int x=n&1; num=num+x; n=n>>>1; } System.out.println("Number of one bits:"+num); } public static void printNumUnitBits(int n){ int num=0; for(int i=0;i<32;i++){ num=num+n&1; n=n>>>1; }

java.util.BitSet — set() doesn't work as expected

戏子无情 提交于 2019-12-01 16:55:01
Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet? The following test fails: @Test public void testBitSet() throws Exception { BitSet b = new BitSet(); b.set(0, true); b.set(1, false); assertEquals(2, b.length()); } It's really unclear to me why I don't end up with a BitSet of length 2 and the value 10. I peeked at the source for java.util.BitSet, and on casual inspection it seems to fail to make sufficient distinction between a bit that's been set false and a bit that has never been set to any value... (Note that explicitly setting the

Why “010” equals 8?

流过昼夜 提交于 2019-12-01 16:01:57
问题 My simple question is why: System.out.println(010|4); prints "12"? I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number? 回答1: Have a look at the Java Language Specification, Chapter 3.10.1 Integer Literals An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8) , or binary (base 2). [...] An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII

Bitwise exclusive OR in Oracle

十年热恋 提交于 2019-12-01 15:51:43
问题 In SQL Server I have been using the ^ symbol, however that doesn't seem to work in Oracle . How do I do a bitwise exclusive OR in Oracle? 回答1: From the docs: function bitor(p1 number, p2 number) return number is begin return p1-bitand(p1,p2)+p2; end; function bitxor(p1 number, p2 number) return number is begin return bitor(p1,p2)-bitand(p1,p2); end; To see that these work, follow the logic with just 0s and 1s for input, and then not that there are no borrow or caries. -- MarkusQ 回答2: There is

How to bitwise shift in VB.NET?

痴心易碎 提交于 2019-12-01 15:51:20
How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method? Mehrdad Afshari VB.NET has had bit shift operators ( << and >> ) since 2003. Robinicks You can use the << and >> operators, and you have to specify how many bits to shift. myFinal = myInteger << 4 ' Shift LEFT by 4 bits. myFinal = myInteger >> 4 ' Shift RIGHT by 4 bits. You can also use it as a unary operator... myFinal <<= 4 ' Shift myFinal LEFT by 4 bits, storing the result in myFinal. myFinal >>= 4 ' Shift myFinal RIGHT by 4 bits, storing the result in myFinal. 来源:

Reverse a byte using assembly language

大城市里の小女人 提交于 2019-12-01 15:35:34
问题 I'm in a microprocessors class and we are using assembly language in Freescale CodeWarrior to program a 68HCS12 micro controller. Our assignment this week is to revers a byte, so if the byte was 00000001, the output would be 10000000, or 00101011 to 11010100. We have to use assembly language, and were told we could use rotates and shifts (but not limited to!) to accomplish this task. I'm really at a loss as to where I should start. 回答1: If you can spare the 256 bytes extra code size, a lookup

Is there a more efficient way of expanding a char to an uint64_t?

孤者浪人 提交于 2019-12-01 15:33:21
问题 I want to inflate an unsigned char to an uint64_t by repeating each bit 8 times. E.g. char -> uint64_t 0x00 -> 0x00 0x01 -> 0xFF 0x02 -> 0xFF00 0x03 -> 0xFFFF 0xAA -> 0xFF00FF00FF00FF00 I currently have the following implementation, using bit shifts to test if a bit is set, to accomplish this: #include <stdint.h> #include <inttypes.h> #define BIT_SET(var, pos) ((var) & (1 << (pos))) static uint64_t inflate(unsigned char a) { uint64_t MASK = 0xFF; uint64_t result = 0; for (int i = 0; i < 8; i+

Standard (cross-platform) way for bit manipulation

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:28:11
As are are different binary representation of the numbers (for example, take big/little endian), is this cross-platform: // NOTE: FIXED-SIZE unsigned integral type some_unsigned_type variable = some_number; // set n-th bit, starting from 1, // right-to-left (least significant-to most significant) variable |= ( 1 << ( n - 1 ) ); // clear the same bit: variable &= ~( 1 << ( n - 1 ) ); In other words, does the compiler always take care of the different binary representation of the fixed size unsigned numbers, or it's platform-specific? And what if variable is signed integral type (for example,

Standard (cross-platform) way for bit manipulation

点点圈 提交于 2019-12-01 14:19:57
问题 As are are different binary representation of the numbers (for example, take big/little endian), is this cross-platform: // NOTE: FIXED-SIZE unsigned integral type some_unsigned_type variable = some_number; // set n-th bit, starting from 1, // right-to-left (least significant-to most significant) variable |= ( 1 << ( n - 1 ) ); // clear the same bit: variable &= ~( 1 << ( n - 1 ) ); In other words, does the compiler always take care of the different binary representation of the fixed size

Breaking up a LPARAM variable & looking at groups of bits

会有一股神秘感。 提交于 2019-12-01 14:18:47
I know that a LPARAM variable has certain bits set(inside it) that identify information such as long key presses & etc. when I receive a WM_KEYDOWN event. So I am trying to break up a LPARAM variable & look at groups of individual bit values & groups of bits & that value(for eg looking at the 16th to the 24th bit & the value from that). My Problem: I dont know how to look at individual bits & groups of bits? How do I break up the LPARAM variable & look at bit values (printing it out in binary, hex & decimal). I have this so far, but working at the bit level confuses me alot, so I am unsure if