Hack Forums
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 B
B = A^B // B is now the original A
A = A^B // A is now the original B

Is that what you meant? Or something else?


* - SIM-1866423 - Feb 18, 2016

PHP Code

<?php 
def isodd
( number):
    if (
number&2):
        return 
True
    
else:
        return 
False 

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:  
PHP Code

<?php 
def isodd
( number):
    if (
number&2):
        return 
True
    
else:
        return 
False 
is incorrect,
PHP Code

<?php 
def isodd
( number):
    if (
number&1):
        return 
True
    
else:
        return 
False 
is correct.
Bitwize operations can also be used to store toggleable flags togeather as one number for instance,
(c code)
PHP Code

<?php 
//includes ommited
#define FLAG1 1
#define FLAG2 2
#define FLAG3 4
#define FLAG4 8
#define FLAG5 16
#define FLAG6 32
#define FLAG7 64
#define FLAG8 128
static char flags = (char)0;
void setFlags(char flagsNew){
flags=flagsNew;
}
char getFlags(){
return 
flags;
}
int main(int argc, const char ** argv){
setFlags(FLAG1|FLAG2|FLAG8);
if(
getFlags()&FLAG1){
printf("flag1")
}
if(
getFlags()&FLAG2){
printf("flag2")
}
if(
getFlags()&FLAG3){
printf("flag3")
}
if(
getFlags()&FLAG4){
printf("flag4")
}
if(
getFlags()&FLAG5){
printf("flag5")
}
if(
getFlags()&FLAG6){
printf("flag6")
}
if(
getFlags()&FLAG7){
printf("flag7")
}
if(
getFlags()&FLAG8){
printf("flag8")
}
return 
0;
} 
this will print "flag1flag2flag8"


* - 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.