Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Bitwise-and Bitwise-or


  • Please log in to reply
11 replies to this topic
  • Guests
  • Last active:
  • Joined: --
What kind of calculation are these?
a := 1000
b := 100
msgbox % a&b	;96
msgbox % a|b	;1004

Bitwise-and (&), bitwise-exclusive-or (^), and bitwise-or (|). Of the three, & has the highest precedence and | has the lowest. Any floating point input is truncated to an integer prior to the calculation.

I don't understand it.

Thanks.

Gogo
  • Guests
  • Last active:
  • Joined: --
1111101000  =  1000

   1100100  =   100

-----------

1111101100  =  1004 ; Bit by bit OR

   1100000  =    96 ; Bit by bit AND


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Numbers can be represented in various ways.
The system of numbers used by a large portion of the world are based on 10 symbols per position.
This is called "base 10" or "decimal".
Other "bases" in computer use are: 2 binary, 8 octal, 16 hexadecimal

See these links and there related pages
<!-- m -->http://mathworld.wol...exadecimal.html<!-- m -->
<!-- m -->http://en.wikipedia....iki/Hexadecimal<!-- m -->

The values you entered written in (binary) are
&-----> 0001100000 -----> 96  "AND"
  100-> 0001100100
 1000-> 1111101000
|-----> 1111101100 -----> 1004 "OR"
        ^^^^^^^^^^
each of the columns is a power of 2 with  2^0 on the right

"And" means the result has a 1 in the result column when both inputs have a 1
"or" means the result has a 1 in the result column when either input (or both) has a 1 




  • Guests
  • Last active:
  • Joined: --
So it seems they are used to merge binary numbers. | takes the highest numbers and & takes the lowest. The manual doesn't explain it.

But I think I got it. Thanks.

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
>>So it seems they are used to merge binary numbers.
It is used to compare, mask, or set bit values.

>>| takes the highest numbers and & takes the lowest.

No, what these operators do is to compares the two numbers at the bit level and provide results as described previously.

The phrase "takes the highest" implies using a member from an existing list.

One use of these operators is separating the R,G & B values from the results of "pixelgetcolor".

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
As background bitwise operators operate at the "bit" level of programming. Bits are the smallest unit of storage a computing machine is capable of. All machine instructions, code, data, video graphics, keyboard characters, everything in a computer is composed of groups of bits arranged in various ways. A bit is actually so small that it can only hold two distinct values a 0 or a 1. Amazingly, by putting bits together in groups you can represent virtually any conceivable size number. This characteristic of being able to hold only 2 values (0 or 1) is called "binary" representation.

To understand bitwise operators you need to understand that when a computer stores data or code it has to put them in computer memory somewhere. That computer memory is made up of things called "gates" and storage units called "flip-flops" that can be wired together in certain ways to make bigger units of storage. When a computer retrieves that information it retrieves them in bits lumped together into things called "nybbles"(4bits in a chunk), "bytes"(8bits in a chunk), "words"(16bits in a chunk), "double-words"(32bits) and even larger units. Regardless of how many bits have been put together to make larger units of information, the bitwise operators operate on only 1 bit at a time. But they can also be combined in various ways to make "nybble operators" "byte operators", "word operators" etc.

The easiest way to describe how bitwise operators work is through the use of what are called "bit tables" each bit table describes an operation that can be done on a single bit using another bit as a pattern. Although there may be some disagreement between computer professionals as to how many absolutely rock bottom simple bit operations there truly are the consensus is that there are only 3 that are what are called "primitive" bitwise operators -- the AND(&) operator, the OR(|) operator, and the NOT(~) operator. All other bitwise operators can be constructed using combinations of just those 3 alone.

Bitwise operators actually need 2 bits to work properly -- one for the item to be managed or altered in some way and one for the pattern that the operator will use to tell it how to change the first bit. The Not operator is the only exception to the rule of using 2 bits for the operation. I know, it seems confusing but an example may help.
for example the bit table for an "and" bitwise operator is..
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1For the or operator the table is slightly different
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1For the not operator the table is much different
~0 = 1
~1 = 0At this point we get into the horrible world of "boolean" or "bitwise algebra" As complicated as that may sound, all that means is that we use letters like A and B for the bit groups we intend to use instead of the numbers. That substitution helps to clarify some more advanced rules for the way these bit groups are combined and manipulated later on. So an AND operator can be redescribed as C = A & B where A is some grouping of bits and B is the pattern to be used for tweaking A. Likewise the OR operator can be descibed as C = A | B. And the NOT operator would become B = ~A.
You can find lots more information on this stuff at http://en.wikipedia....twise_operation and yes it can get a little overwhelming if you try to digest it all at once.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
What a terribly in depth and simplified explaination thank you very much girlgamer. Your are an asset to this community
Never lose.
WIN or LEARN.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
I try. Sometimes i get a little wordy. I guess it comes from all those years teaching computer maintenence, digital design, and programming. One just can't seem to live down one's past.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
There is also an article in the ahkbook about this.

  • Guests
  • Last active:
  • Joined: --
Thanks guys for the replies.

>>| takes the highest numbers and & takes the lowest.

No, what these operators do is to compares the two numbers at the bit level and provide results as described previously.

The phrase "takes the highest" implies using a member from an existing list.

I don't see a problem with what I said previously. | takes the highest number in each field and & take the lowest. And it seems to be correct as shown in the below code. Also it seems to be not necessarily to compare only two values but can handle more than two values.
A := 100 ;0001100100
B := 256 ;0100000000
C := 512 ;1000000000

;0001100100
;0100000000
;1000000000
;---------- take the highest number in each column
;1101100100	-> decimal 868
msgbox % A|B|C

;0001100100
;0100000000
;1000000000
;---------- take the lowest number in each column
;0000000000 -> decimal 0
msgbox % A&B&C


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
>>Also it seems to be not necessarily to compare only two values but can handle more than two values.

As with the adding of numbers, you can AND and OR as much as to want.
With processors, the values are taken 2 values at a time.


Since there are only 2 possible choice per bit, your phrases "take the highest" and "take the lowest" happens to coincide with the results.

However, it is not the correct understanding.

In the world of binary, the term "highest" is commonly used to denote the bit of greatest significance.
In electronics, involving binary there are even more variants.

girlgamer has already given you a good treatis on the subject.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
The problem with trying to simplify the results by saying AND = take the lowest and OR = take the highest becomes extremely awkward and error prone once you get past simple gates. For example the bit table for the XOR function and even higher level bitwise constructs becomes unwieldy.
Just as an example the XOR function bit table consists of
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0It's verbal description being C = either A OR B but not both
the actual structure of a bitwise XOR in boolean algebra is thus
C = (A & ~B) | (~A & B) and yes, while it works using your methodology, the problem becomes the mental gymnastics required for determining the "lowest" and "highest" values in complex functions with multiple operators.

Unfortunately things get really messy when dealing with multiple operators and data in the same expression. For more on this see
http://en.wikipedia....algebra_(logic)

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif