bitwise-operators

Resource(s) for learning bitwise operation?

I was recently asked a question, "how do you multiply without using the multiplication operator, without any sort of looping statements or explicit addition" and realized I wasn't familiar with bitwise operation at all. There is obviously wikipedia but I need something with more of an explanation geared toward a newbie. There's also thi...

Bitwise AND (&) expression in Java

I am debugging code that has in it expr1 & expr2 where expr1 has a side effect that affects expr2 evaluation result. I suspect that expr2 gets evaluated before expr1, since JLS guarantees left-to-right evaluation for &&, but not necessarily for &. I also suspect that change of evaluation order may be a result of optimization performed by...

Does J have a built-in bitwise xor primitive?

I know J has a primitive that works like xor ~:, but this is really a not equal to (!=) I can make it function like a bitwise xor by saying:xor =: 4 : '#.((#:x)~:(#:y))' within a verb definition, but this only works when the binary representations of the numbers are the same length. Is there anything I can do short of making a full-out ...

What does "|=" mean in Java?

Note my question is not regarding != but |= A usage example is here I assume that x |= y is the same as x = x | y but I could not find confirming documentation and wanted to be sure Thanks ...

In ASP, Bit Operator Left shift and Right shift

Hi, Does anyone know left shift and right shift operator sample's? I'm new in ASP. I found Bit operators such as AND,OR,NOT only.. ...

Rotate Bits Right operation in Ruby

Hi all, Is there a Rotate Bits Right in Ruby ? Or how can I do that please. Thanks ...

Bitwise operation error ?

I'm developping a site for fun and I'm trying to implement a directory access control based on bitwise operators. I've defined GUEST = 1, GROUP1 = 15 and GROUP2 = 23 If I compare echo (23 & 1); // print 1 but if I define GUEST, GROUP1 and GROUP2: define('GUEST', 1); define('GROUP1', 15); define('GROUP2', 23); // and then ...

C++: Union vs Bitwise operators

Hi, I have two char's and I want to "stitch" them bitwise together. For example: char c1 = 11; // 0000 1011 char c2 = 5; // 0000 0101 short int si = stitch(c1, c2); // 0000 1011 0000 0101 So, what I first tried was with bitwise operators: short int stitch(char c1, char c2) { return (c1 << 8) | c2; } But this doesn't work: I'm...

MySQL doesn't use indexes when query over BIT field using bitwise functions

I have a field of type BIT in my MySQL table. I want to store statuses of the record using bit value, for example: 1 = status1 2 = status2 4 = status3 8 = status4 Each record can have many statuses at once. For status1 and status3 the value will be 1 + 4 = 5. I can query table for all records with status3 using: SELECT * FROM `tab...

I don't understand the following C code line

I found the following thread: http://stackoverflow.com/questions/777617/calculate-broadcast-address-from-ip-and-subnet-mask and there the link to http://lpccomp.bc.ca/netmask/netmask.c Could someone please explain the following line, I don't understand: for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- ) especially...

Is there a built-in function to reverse bit order

I've come up with several manual ways of doing this, but i keep wondering if there is something built-in .NET that does this. Basically, i want to reverse the bit order in a byte, so that the least significant bit becomes the most significant bit. For example: 1001 1101 = 9D would become 1011 1001 = B9 On of the ways to do this is to...

why are Javascript negative numbers not always true or false?

-1 == true; //false -1 == false //false -1 ? true : false; //true Can anyone explain the above output? I know I could work round this by comparing to 0 but I'm interested. I'd expect at least one of the sloppy equals statements to be true as they do implicit type conversion, and I certainly didn't expect the ternary to co...

bitwise not (~) operator, clarification needed

Suppose you have the following C code unsigned char a = 1; printf("%d\n", ~a); // prints -2 printf("%d\n", a); // prints 1 I am surprised to see -2 printed as a result of ~1 conversion: Opposite of 0000 0001 is 1111 1110 --> anything but -2 What am i missing here? please advise ...

Logical NOT (!) operator won't work with bitwise statement

I am attempting to determine if I can compute the sum of two 32 bit integers without overflow, while making use of only certain bitwise and other operators. So, if the integers x and y can be added without overflow, the following code should return 1, and 0 otherwise. (((((x >> 31) + (y >> 31)) & 2) >> 1)) However, it returns 0 when ...

How to make an Encode function based on this Decode function?

How to make an Encode function based on this Decode function? I got the source code for the Decode function on the internet but I need the Encode function. All my attempts to make it failed and the original coder isn't available at the moment. The (original) code: byte Decode(byte EncodedByte) { EncodedByte ^= (byte)194; Encod...

Bitwise operator predence

A gotcha I've run into a few times in C-like languages is this: original | included & ~excluded // BAD Due to precedence, this parses as: original | (included & ~excluded) // '~excluded' has no effect Does anyone know what was behind the original design decision of three separate precedence levels for bitwise operators? More im...

How can I check if a signed integer is positive?

Using bitwise operators and I suppose addition and subtraction, how can I check if a signed integer is positive (specifically, not negative and not zero)? I'm sure the answer to this is very simple, but it's just not coming to me. ...

JavaScript Bitwise operations

Hi, I would like to store all currently-pressed keycodes in a single variable by using bitwise operations when the key is pressed and when the key is released. I'm not sure how to properly use bitwise operations, but I know this will be very simple to someone who does. Once complete, it should be simple to see which key is currently d...

C# bitwise shift on ushort (UInt16)

I need to perform a bitwise left shift on a 16-bit integer (ushort / UInt16), but the bitwise operators in C# seem to apply to int (32-bit) only. How can I use << on an ushort, or at least get to the same result with a simple workaround? ...

Can one assign 4 little-endian-ordered bytes of an unsigned integer to a Java primitive using just bitwise operators?

I want to read 4 bytes which are a little-endian encoding of an unsigned 32-bit integer, and assign the value to a Java int (Yes, in reality I will use a 'long', but in this case I 'know' that the unsigned value is never so big that it will overflow a signed int in two's complement notation, and it suits my illustration to use an int)....