d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Give An Indepth Explanation
Prev12
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jun 21 2016 03:39am
Thanks for the article Abduct
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 21 2016 06:56pm
If you're truly interested in bitwise stuff google/youtube has a bunch of stuff. Most people don't explain it very well though.

One video that did an okay job is https://www.youtube.com/watch?v=Nm4u3kCEKo8

To be honest you need to find one that shows the before operations in binary and after result in binary, so you can physically see the results and the bits moving around. I haven't found such video yet as most people skim right past everything without explaining the core concept.

edit:: At the end of that video it quickly dives into bitwise flags. These could be useful for a lot of things. On/off options, positional checks, etc.

For example say you have a array with 8 elements. You could use a 1 byte (8 bit) number to represent if that specific element is used or not.

Pseudo code, don't shoot me.

Code
array = Array(8).new
bitmast = Byte.new;

array[3] = "this is used"
bitmask ^= 0b0000_1000 #=> bitmast is now 0000_1000

array[7] = "this is now used"
bitmask ^= 0b1000_0000 bitmast is now 1000_1000


Then rather than iterating the entire array to find if a position is filled you can either poll each bit position checking for 1, or return all full bits and iterate the bit mask looking for the first free space:

Code
#is position 1 full?
if(bitmask & 0b0000_0010) {
#no its not
} else {
#yes it is
}

bitcopy = Byte.new;
bitcopy = bitmask #make a copy of the bitmask
for(int i = 0; i <= 8; i++) { #iterate each position of the bitmask
if(botcopy & 0b0000_0001) { #if our current index is full
bitcopy = bitcopy >> 1; #shift our bitmask over 1 bit so we can check the next element
next;
} else {
#i holds the index that is free; #the bitmask held a 0 so that element is empty.
}
}


Sure this isn't that useful in a structured language but it is another use once you start doing networking or other technical programming.

Unfortunately you can't easily visualize how the bits are moving unless seeing a live demo.

This post was edited by AbDuCt on Jun 21 2016 07:09pm
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Jun 28 2016 03:59am
THE MAIN DIFFERENCE BETWEEN THESE OPERATORS IS NOT THAT ONE IS SHORT-CIRCUIT AND THE OTHER ISN'T.

It is indeed one of the differences, which is a logical result of how they work. && and || are logical (i.e. returning a boolean) while & and | are bitwise operators.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 28 2016 09:54am
Quote (howtodisappearcompletely @ Jun 28 2016 05:59am)
THE MAIN DIFFERENCE BETWEEN THESE OPERATORS IS NOT THAT ONE IS SHORT-CIRCUIT AND THE OTHER ISN'T.

It is indeed one of the differences, which is a logical result of how they work. && and || are logical (i.e. returning a boolean) while & and | are bitwise operators.


Except that & and | can also return booleans when used as logical operators.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 28 2016 09:54am
Quote (howtodisappearcompletely @ Jun 28 2016 05:59am)
THE MAIN DIFFERENCE BETWEEN THESE OPERATORS IS NOT THAT ONE IS SHORT-CIRCUIT AND THE OTHER ISN'T.

It is indeed one of the differences, which is a logical result of how they work. && and || are logical (i.e. returning a boolean) while & and | are bitwise operators.


Except that & and | can also return booleans when used as logical operators.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll