Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Bitwise Operators

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?
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?
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...EADING2-49
(Feb 18, 2016 10:53 AM)SIM-1866423 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"
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Help] by schassisAug 27, 2025 05:53 PM 3 332 Aug 27, 2025 05:53 PM
Last Post: webdev77
by James BurnhamAug 2, 2020 04:47 PM 0 84 Aug 2, 2020 04:47 PM
Last Post: James Burnham
[Delphi] by PowdersnowJun 25, 2015 09:56 AM 1 249 Jun 25, 2015 09:56 AM
Last Post: Volty