Jump to content

Bitwise Operators


Fmdpa

Recommended Posts

I'm having trouble grasping the concept of bitwise operators. For example, I know that "<<" is supposed to shift the bits left, but exactly what does that mean? What would be a practical application of bitwise operators?

Link to comment
Share on other sites

I'm having trouble grasping the concept of bitwise operators. For example, I know that "<<" is supposed to shift the bits left, but exactly what does that mean? What would be a practical application of bitwise operators?
00110011 << 1 = 01100110or in decimal51 << 1 = 10200110011 >> 1 = 00011001in decimal51 >> 1 = 25Bitwise AND, OR and XOR can be used for bitmasking. You don't see the bitwise shift operators used very often.
Link to comment
Share on other sites

Bit shifts are common in various encryption or hashing algorithms, SHA1 does a lot of bit shifting. The algorithm for that is here, when they use leftrotate they're talking about a left shift.http://en.wikipedia.org/wiki/Sha1It also happens to be the quickest way to multiply or divide by a power of 2.

Link to comment
Share on other sites

0b10 = 2, so you just have to multiply it by two again. If you wanted to square a non-power of two, then it's much harder.0b00000010 << 1 = 0b00000100

Link to comment
Share on other sites

00000010 << 1 = 00000100
Or, if you were thinking of 10 to mean the likely number of your fingers or toes, and you wanted to know how many of both you likely have, then you would perform the following:00001010 << 1 = 00010100In other words, every time you shift everything one bit to the left you multiply whatever you originally had by the number 2.Always keep in mind that binary means base 2.Roddy
Link to comment
Share on other sites

0b00000010 << 1 = 0b00000100
Synook, what does the 0b mean?Is it a standard prefix for binary numbers in an 8-bit system?Roddy
Link to comment
Share on other sites

Many languages use that notation for indicating binary numbers of any length (akin to 0x for hexadecimal) - PHP doesn't seem to have it though.

Link to comment
Share on other sites

This is fascinating, yet challenging. First of all, why are there assignments being made in the bit shift?

0b00000010 << 1 = 0b00000100

Also, I was playing around with the negation operator (~), but it didn't seem to work like I read. From what I learned, it is supposed to invert all of the binary numbers, ie 10100 to 01011. But instead, it seems to be negating the number, then adding one to it.

echo ~1001; //prints -1002

It doesn't seem to be respecting it as a binary string, but rather, one large number. Do I need to use a particular function rather than the echo construct to output binary?

Link to comment
Share on other sites

Sorry, I was attempting a pseudo-code representation, to indicate that 2 left shift 1 is equal to 4. To indicate a number is binary in PHP, you have to write b"1001".

Link to comment
Share on other sites

It doesn't seem to be respecting it as a binary string, but rather, one large number. Do I need to use a particular function rather than the echo construct to output binary?
Right, it's using one thousand and one. It doesn't treat a number as binary, even if it's only 0s and 1s. It's easiest to work in hex instead of binary. 1001 in binary is 0x9, so if you printed ~0x9 then you should get 0x6, which is 0110. To convert between everything for calculation and display, you can use functions like decbin and bindec to convert between base-2 and base-10, hexdec and dechex for base-10 to base-16 conversion, base_convert for arbitrary base conversion, and printf or sprintf to output the number as a binary string. Do the math using hex numbers and then format it to print using printf or sprintf.
Link to comment
Share on other sites

This is pretty neat! I just wrote a function to add numbers without any "plus" operators.

function add($num) {   if (is_array($num)) {	  $result = '<ul>';	  foreach($num as $one => $two) {		 $result .= '<li>';		 $result .= $one^$two;		 $result .= '</li>';	  }	  $result .= '</ul>';	  return $result;   }}

Still trying to grasp the other bitwise operators. When using printf(), Do you print the binary number in the string with a %d or a %b?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...