answersLogoWhite

0

How do you reverse bits using bitwise operator in PHP?

Updated: 8/17/2019
User Avatar

Wiki User

13y ago

Best Answer

The easiest way to do this is with an exclusive-or operator, represented by the ^ symbol.

For example, 10 ^ 15 = 1010b ^ 1111b = 0101b

Here's a quick example script that would make use of it:

#!/usr/bin/php

<?php

if($argc != 2){ echo "gimme a number!\n"; exit;}

$x = $argv[1];

for($n = 0; 1 << $n <= $x; $n++);

$y = $x ^ ((1 << $n) - 1);

echo "The binary complement of $x = %y\n";

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you reverse bits using bitwise operator in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the definition of bitwise?

Bitwise operations are those that operate on one or more bits of data, as opposed to larger units of data. For example, in C++, there are the bitwise operators "^" (exclusive or), "&amp;" (and), "|" (or), and "~" (bitwise complement). Using these symbols, it is possible to determine if a bit is set or unset, combine sets of bits, find common bits, or invert all the bits at once. This allows the use of a single field (such as an int variable) to hold multiple pieces of data. Algorithms that concern themselves with saving space, such as compression algorithms, embedded system code, and so on, will often use bitfields instead of entire bytes of data whenever practical.


What are Bit Manipulations in C programming?

in C you can manipulate the individual bits of a data item. &lt;&lt; leftwise shift &gt;&gt; rightwise shift &amp; bitwise AND | bitwise OR ~ bitwise complement (flips all bits) I can't go intoa tutorial here in how to use these, but google it - i just did and there's tons of info out there.


What is the use of complementry operator in c?

The bitwise complement or one's complement operator (~) is used to switch the state of all the bits in a value. Thus 1's become 0, and 0's become 1. One of its many uses is to unset individual bit(s) in a bitmap. We do this with a bitwise AND of the bitmap and the bitwise complement of the bit(s) we want to unset. Original bitmap: 01011100 Bit to unset: 00000100 (e.g., bit 2 (bits are zero based from right)) // Using one's complement and bitwise AND ~00000100 &amp; 01011100 11111011 (one's complement of bit 2) &amp; 01011100 (original bitmap) = 01011000 (original bitmap with bit 2 unset) Note that this formula works even if bit 2 were already unset: 11111011 (one's complement of bit 2) &amp; 01011000 (original bitmap, with bit 2 unset) = 01011000 (original bitmap unchanged)


What is a bit shift?

A bit shift is a bitwise operation in which the bits in a value are shifted left or right.


What is the difference between bit wise operator and logic operator?

The logic operator provides boolean results of combinations of other boolean expression, some of which might be relational expressions. For example... bool result = (a &lt; 3) &amp;&amp; (b &gt; 4); The bitwise operator provides the same kind of boolean logic, AND, OR, and NOT, but it does it to the correspondingly ranks bits in one or two integers. For example ... int result = (a &amp; 0xff) | (!b);


Why do you need to learn how to AND?

The AND operator is a binary operator, thus it accepts two operands. However, the type of the operands depends upon whether you mean the logical AND or the bitwise AND. The logical AND (&amp;&amp; in C/C++) accepts two boolean expressions, returning true if and only if both expressions evaluate true. If either or both evaluate false, the return is false. int a=42, b=69; assert (a==42 &amp;&amp; b==69); // e.g., true AND true == true The bitwise AND (&amp; in C/C++) does the same thing but at the bitwise level, comparing each of the corresponding bits of each operand. If both bits are 1, then the output for that bit is 1, otherwise the output is 0. You do this to determine which specific bits appear in both operands. For this to work, both operands must be of the same type and length (in bits). int a=42, b=69, c=a &amp; b; assert (c==0); In binary: a (00101010) &amp; b (01000101) = c (00000000)


What is the use of Bitwise operators?

AnswerThe bitwise operators treat a number as its binary equivalent rather than as a simple boolean value.For most programming languages, a value of zero is considered FALSE and all other values are TRUEThus, 8 AND 11 returns TRUE as does 3 OR 0In bitwise analysis, each binary bit of the digit are compared. The number of bits compared will depend on the type of number.In C, a CHAR is usually 8 bits and can hold the binary numbers 0 to 255.If we compare 8 (00001000) and 19 (00010011) with bitwise operators, we get different results from Boolean operators:8 BITWISE AND 19 returns 0 (each bit in the response is set to 1 if both equivalent bits compared are 1) but 8 BITWISE OR 19 will return 27.The utility of these methods is in identifying binary data. For example, all files on a PC have the characteristics 'Hidden' 'Read Only' 'Archive' and 'System' which can be set or unset using bitwise operations on a single byte of data. In truth this is a throwback to the days of small memory capacities where saving the odd byte was essential.There are more uses of bitwise, especially in graphics, where XOR can be used to paint a sprite image to display it and then be used again to return a background to its former settings. I regret I lack the skill to explain this better.


Define unary operator explain different unary operators used in c language?

A unary operator is one that requires only one operand. The unary operators are the prefix and postfix increment and decrement, positive and negative, logical and bitwise negation, and address and indirection operators, plus the words alignof, typeof, and sizeof.The first two operators are increment "++", and decrement "--". Its position before or after the variable determines prefix or postfix. Incrementing increases the stored value by 1, and decrementing decreases the stored value by 1. If it is to the left of the variable, the operation occurs before the variable is used; if it is to the right, it occurs after the variable is used.The positive operator "+" simply asserts the current value, and exists as acomplementto the negation operator "-". The negation operator returns the number with the opposing sign. If x is 5, then -x is -5, and if x is -5, then -x is 5.The logical negation "!" and bitwise negation "~" operators perform actions on individual bits. C considers zero to be false, and all other values to be true. Using logical negation, it returns true if the value is zero, or false for any other value. The bitwise negation changes all 1 bits to 0 bits, and 0 bits to 1 bits. For an unsigned byte, ~0 becomes 255, and ~255 becomes 0. For signed variables, ~0 would become -1, and ~-1 would become 0. This is because of the two's complement method of signed numbers.Address "&" and indirection "*" operators take the address or value of the operand. The address operator allows a variable to be passed by reference; modifying the reference will modify the original value. Using the indirection operator treats a variable as a memory address, which allows the programmer to access a specific spot in memory.Alignof, sizeof, and typeof are all used to determine the alignment, size, and type of objects dynamically. This is necessary when trying to determine how data is laid out when there may be differences in memory accesses across platforms (e.g. when reading a ZIP file's directory contents).


What is tilde operator in c and how its work?

In C, the tilde operator (~) is the bitwise NOT operator. It returns the ones-complement of its operand. That is, the individual bits of the input are inverted in the output, such that all 0s becomes 1s and all 1s become 0s. Note that the bitwise NOT (~) and logical NOT (!) operators are used for entirely different purposes. With logical NOT, the operator evaluates true (the all-ones bit pattern) when the operand is false (the all-zeroes bit pattern), which is exactly the same as the ones-complement used in bitwise NOT. However, if the operand represents anything other than the all-zeroes bit pattern, the output is the all-zeroes bit pattern. We can compare the two operators by examining what happens to the bits in each operation. Let's use the value 42 (binary 00101010) as the input. ~42 -43 !42 false Note that the binary value 11010101 represents -42 on a ones-complement system. However, most systems today use twos-complement notation for signed values, thus if we want to negate a value regardless of which notation is utilised by the system we must use the unary minus operator. On a twos-complement system, unary minus is equivalent to adding 1 to the ones-complement representation of the operand. Thus -42 is equivalent to (~42) + 1 = (~00101010) + 00000001 = 11010101 + 00000001 = 11010110 = -42.


What is difference between conditional operator and bitwise operator?

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.


Write a program to illustrate bitwise operators without swap?

#include&lt;stdio.h&gt; int main() { int n,n2; printf("enter the no. &lt; 15 "); // here i am considering the case of 4 bits. (1111) binary = (15) decimal scanf("%d",&amp;n); n2=n^10; /* 10 = 1010 in binary form, to invert its even bits , we will use bit wise XOR (^) operator 1010 has 1 at its even places, so it will invert the even bits of n. if there is any further problem mail me at buntyhariom@gmail.com www.campusmaniac.com */ printf("\n%d",n2); return 0; }


What is the purpose of and in c language?

Bitwise AND (&amp;) is used to return a bitmap value indicating which bits are set in both operands. If both bits are set, the corresponding bit in the bitmap is set, otherwise it is unset. Logical AND (&amp;&amp;) is used to return true when both operands are true. If either or both are false, the return is false.