d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Give An Indepth Explanation
12Next
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jun 20 2016 04:19pm
Of the differences, between (AND/OR)

&, |


and && and ||


(Short circuit AND/OR) operators.... Would really appreciate it, not sure i fully understand it from the book i'm reading
^in java that is

This post was edited by ferf on Jun 20 2016 04:48pm
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 20 2016 04:56pm
& is bitwise AND, while && is logical AND. Same goes for the OR operators. I wouldn't worry about the bitwise operators and just focus on using the logical operators properly in your conditionals/logic.

Bitwise ANDs/ORs are used in performing operations on bits like 0011 AND 1010. These operators are mainly used for lower level programming languages and not languages like java (generally).

This post was edited by umeshieee on Jun 20 2016 05:16pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jun 20 2016 05:00pm
Quote (umeshieee @ Jun 20 2016 06:56pm)
& is bitwise AND, while && is logical AND. Same goes for the OR operators. I wouldn't worry about the bitwise operators and just focus on using the logical operators properly in your conditionals/logic.


idk what is difference between bitwise and logical... I'm pretty sure i know what logical is but... and why not worry about bitwise?
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 20 2016 05:02pm
Quote (ferf @ Jun 20 2016 07:00pm)
idk what is difference between bitwise and logical... I'm pretty sure i know what logical is but... and why not worry about bitwise?



Edited my comment for you.
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 20 2016 05:04pm
I think I can confidently say you don't have to worry about bitwise operators until you start learning about machine language and what is actually happening behind the scenes of higher level languages. Someone else can correct me if I am wrong

This post was edited by umeshieee on Jun 20 2016 05:05pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jun 20 2016 05:10pm
Quote (umeshieee @ Jun 20 2016 07:04pm)
I think I can confidently say you don't have to worry about bitwise operators until you start learning about machine language and what is actually happening behind the scenes of higher level languages. Someone else can correct me if I am wrong


Hmmm, i think it might be different with java, but you could be right.... thanks for input... looking for someone to clarify that knows java, just incase! thanks again !
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 20 2016 05:37pm
short answer: && and || will short circuit, the others will not.

instead of typing everything out, here's a link: http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting

Quote (umeshieee @ Jun 20 2016 07:04pm)
I think I can confidently say you don't have to worry about bitwise operators until you start learning about machine language and what is actually happening behind the scenes of higher level languages. Someone else can correct me if I am wrong


bitwise operators can work with bits. it's not really related to machine language. flags are a common reason to use bitwise operators. for example, you could have 5 columns in your database to represent 5 flags. downside is you have to add columns to your table every time you want a new flag. instead, you can fit a single integer into the column and use bitwise operators. (you can also just use it as a NNYYNN string in a single column)

with that said, i've already told him not to worry about bitwise operators yet. i felt learning OOP is more important in comparison, but he doesn't listen to my advice. *shrug*

This post was edited by carteblanche on Jun 20 2016 05:42pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Jun 20 2016 05:55pm
thanks guys
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 20 2016 06:04pm
Quote (carteblanche @ Jun 20 2016 07:37pm)
short answer: && and || will short circuit, the others will not.

instead of typing everything out, here's a link: http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting



bitwise operators can work with bits. it's not really related to machine language. flags are a common reason to use bitwise operators. for example, you could have 5 columns in your database to represent 5 flags. downside is you have to add columns to your table every time you want a new flag. instead, you can fit a single integer into the column and use bitwise operators. (you can also just use it as a NNYYNN string in a single column)

with that said, i've already told him not to worry about bitwise operators yet. i felt learning OOP is more important in comparison, but he doesn't listen to my advice. *shrug*



That's why I wanted a second opinion. I agree, learn good OOP principles and pick up a framework like spring.
Also learn junit testing :)

This post was edited by umeshieee on Jun 20 2016 06:11pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 20 2016 07:21pm
Quote (umeshieee @ Jun 20 2016 06:56pm)
Bitwise ANDs/ORs are used in performing operations on bits like 0011 AND 1010. These operators are mainly used for lower level programming languages and not languages like java (generally).


I wouldn't say that. You can use bitwise manipulations in any language for a multitude of things. Such as truth tables and networking protocols. Hell if you ever have to write your own serializer you may want to use them.

In the networking example say you needed to add the length of the packet to the first 2 bytes of the message. You simply can't insert the decimal (when working with raw bytes) so you would have to use bit shifts and bitwise ands to insert the value into the two bytes. Well I guess you could avoid them if you use a higher level engine which automatically does these things for you, but there is no reason not to learn it as they are not difficult.

Code
packet[0] = (0xff00 & (length)) >> 8;
packet[1] = (0xff & (length));


Let's add some values to this. Let Length be 1500 (0x05DC). The first operation does a bitwise and against 0xFF00. When we view this at the binary level we can see whats happening

Code
(FF00) 1111 1111 0000 0000
(05DC) 0000 0101 1101 1100
-----------------------------------
(0500) 0000 0101 0000 0000


If we recall "and" means "if both bits/operands are true (1) then the result is (1), else the result is 0". From this we can tell that by using the magic number we did, we could specify which bits we wanted to keep and discard the rest. The magic number is usually 0's and F's because they represent all 1's or all 0's in binary. In this case we kept the first 8 bits which which returned 0000 0101 0000 0000, skipping the first four, 1&0 = 0, 1&1 = 1, 1&0=1, 1&1=1 following the rules of the bitwise and.

Next we shift our number 8 bits to the right so our value we extracted from our original length is 0x05. The process is repeated to extract the bottom half of our length to fill our other byte in our packet.

More on magic numbers:

Say we wanted to extract the last half byte of data from 0x05DC, we could specify 0x000F as our magic number. In turn this equates to:

Code
(FF00) 0000 0000 0000 1111
(05DC) 0000 0101 1101 1100
-----------------------------------
(000C) 0000 0000 0000 1100


------------------------------------------------------------------------------------

Trueth tables using flags are great as well if you have a bunch of tasks that are required to be based off one another, but can't fit into clean if/ if else/ else logic. http://smyck.net/2015/12/10/truth_tables/ this article explains truth tables quite well.

------------------------------------------------------------------------------------

For ferf, I suggest taking a read of this http://pastebin.com/eqsL0v8g . It's an old paper, but it walks you through decimal, binary, and the bitwise operations.

This post was edited by AbDuCt on Jun 20 2016 07:46pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll