![]() |
|
Bitwise Operators - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: Bitwise Operators (/showthread.php?tid=5175088) |
Bitwise Operators - Ugleh - Feb 18, 2016 This might sound like a stupid question but I have always had problems understanding Bitwise Operators and WHY we need them. I have been programming for over 10 years. I know what Bitwise Operators do so maybe I have just done stuff the hard way where I could have easily done it through Bitwise Operators. Could someone give me an EL15 example of Bitwise Operators? * - Annabelle Lee - Feb 18, 2016 There can be several reasons to use them. Mainly code speedup, as well as when you need to manipulate individual bits. They're useful for setting flags in bitfields and such as well. For example, when you want to multiply or divide by two. You can use a logical shift. (>> or <<) to do it quickly/easily. They can also do some cool things such as stuffing two 16 bit integers in a 32 bit integer. Or swapping variables without using a temp, such as: Code A = A^B // A is now XOR of A and BIs that what you meant? Or something else? * - SIM-1866423 - Feb 18, 2016 PHP Code Try some low level languages you will understand why.Check this out; https://courses.engr.illinois.edu/ece390/books/artofasm/CH01/CH01-2.html#HEADING2-49 * - YJGFrowned - Feb 18, 2016 (Feb 18, 2016 10:53 AM)b3mb4m Wrote:is incorrect, PHP Code Bitwize operations can also be used to store toggleable flags togeather as one number for instance, (c code) PHP Code * - Ozedath - Feb 26, 2016 (Feb 18, 2016 07:54 AM)Ugleh Wrote: This might sound like a stupid question but I have always had problems understanding Bitwise Operators and WHY we need them. I have been programming for over 10 years. I know what Bitwise Operators do so maybe I have just done stuff the hard way where I could have easily done it through Bitwise Operators. Could someone give me an EL15 example of Bitwise Operators? In general purpose computing (PCs) they are used for optimization. A pretty comprehensive list can be found here: https://graphics.stanford.edu/~seander/bithacks.html They are mostly used in embedded systems due to the limited number of resources; all bits can be put to use in variables through bitmasks and shifting operations. |