bitwise-operators

What are these values representative of in C bitwise operations?

Hi All, I'm trying to reverse the order of bits in C (homework question, subject: bitwise operators). I found this solution, but I'm a little confused by the hex values (?) used -- 0x01 and 0x80. unsigned char reverse(unsigned char c) { int shift; unsigned char result = 0; for (shift = 0; shift < CHAR_BITS; shift++) ...

How does the bitwise operator '^' work?

I'm a little confused when I see the output of following code: $x = "a"; $y = "b"; $x ^= $y; $y ^= $x; $x ^= $y; echo $x; //got b echo $y; //got a And I wonder how does the operator ^ work here?Explanations with clarity would be greatly appreciated! ...

Bitwise operators- need pseudocode translation

Hi Could someone translate this statement into pseudo-code (or just plain english)? var c:uint = 0xFF << 24 | r << 16 | g << 8 | b; ...

bitwise operations on strings with ruby

How can I perform bitwise operations on strings in ruby? I would like to do bitwise & a 4-byte string with a 4-byte long hex such as ("abcd" & 0xDA2DFFD3). I cannot get the byte values of the string. Thanks for your help. ...

What is the point of the logical operators in C?

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did. Consider...

C & PHP: Storing settings in an integer using bitwise operators?

I'm not familiar with bitwise operators, but I have seem them used to store simple settings before. I need to pass several on/off options to a function, and I'd like to use a single integer for this. How can I go about setting and reading these options? ...

java help bitwise operations

System.out.println( s + ", long: " + l + ", binary: "); System.out.print(" "); for(int i = 63; i >= 0; i--) if(((1L << i) & l) != 0) System.out.print("1"); else System.out.print("0"); System.out.println(); ...

Bitwise Operations -- Arithmetic Operations..

Can you please explain the below lines, with some good examples. A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow). And: A right arithmetic shift by n of a two's complement value is equivalent to dividing by 2n and rounding toward negative infinity. If the bina...

Using a bitwise AND on more than two bits.

Hi, I am pretty new to bitwise operators. Let's say I have 3 variables a, b and c, with these values in binary: a = 0001 b = 0011 c = 1011 Now, I want to perform a bitwise AND like this: a AND b AND c -------- d = 0001 d &= a &= b &= c doesn't work (as I expected), but how can I do this? Thanks ...

Construction an logical expression which will count bits in a byte.

When interviewing new candidates, we usually ask them to write a piece of C code to count the number of bits with value 1 in a given byte variable (e.g. the byte 3 has two 1-bits). I know all the common answers, such as right shifting eight times, or indexing constant table of 256 precomputed results. But, is there a smarter way with...

bit manipulation in java

I have a fragment of bytes in a byte[]. The total size of the array is 4 and I want to convert this into a positive long number. For example if the byte array is having four bytes 101, 110, 10, 10000001 then i want to get the long number represented by binary sequence 00000000 00000000 00000000 00000000 00000101 00000110 00000010 10000...

Is it possible to implement bitwise operators using integer arithmetic?

Hello World! I am facing a rather peculiar problem. I am working on a compiler for an architecture that doesn't support bitwise operations. However, it handles signed 16 bit integer arithmetics and I was wondering if it would be possible to implement bitwise operations using only: Addition (c = a + b) Subtraction (c = a - b) Division...

How can I access the sign bit of a number in C++?

I want to be able to access the sign bit of a number in C++. My current code looks something like this: int sign bit = number >> 31; That appears to work, giving me 0 for positive numbers and -1 for negative numbers. However, I don't see how I get -1 for negative numbers: if 12 is 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0...

is mysql index useful on column 'state' when only doing bit-operations on the column?

I have a lot of domain entities (stored in mysql) which undergo lots of different operations. Each operation is executed from a different program. I need to keep (flow)-state for these entities which I implemented in as a long field 'flowstate' used as a bitset. to query mysql for entities which have undergone a certain operation I do s...

What good does zero-fill bit-shifting by 0 do? (a >>> 0)

I just came across this piece in the Mozilla Javascript documentation: var len = this.length >>> 0; I don't quite understand why this is being done. What good does zero-fill right shifting this.length by 0 do? As far as I understand, it does exactly nothing. Is it to safely establish a default value for len, even if this.length is n...

Comparing floats using bitwise operators

how to do comparisons of float values using bitwise operator? ...

Getting each individual digit from a whole integer

Let's say I have an integer called 'score', that looks like this: int score = 1529587; Now what I want to do is get each digit 1, 5, 2, 9, 5, 8, 7 from the score using bitwise operators. I'm pretty sure this can be done since I've once used a similar method to extract the red green and blue values from a hexadecimal colour value. How...

Turn on leftmost bit of a Short.

Original question changed. I want to bitwise turn off the left most bit of a Short value (&H8000) and leave the other bits as they are. Dim x = BitConverter.GetBytes(Short.MaxValue Or &H8000) Dim z = BitConverter.ToInt16(x, 0) Isn't there any shorter way with bitwise operators? When I do Dim a = Short.MaxValue Or &H8000 I get a c...

How do you properly void a permission when using Bitwise Permissions?

Alright, Here's the gist of what I'm planning to do. I'm going to have two tables. One with "ranks" or "roles" and one with users. I want to assign permissions on a role/user basis and retract permissions on a user basis. So, just for general purposes lets say that we have $role_can $user_can and $user_cant I know that for specifyin...

Question about bitwise And and Shift operations

How exactly do the following lines work if pData = "abc"? pDes[1] = ( pData[0] & 0x1c ) >> 2; pDes[0] = ( pData[0] << 6 ) | ( pData[1] & 0x3f ); ...