# Getbit opposite function

**URL:** <https://forum.inductiveautomation.com/t/getbit-opposite-function/6220>\
**Category:** Ignition\
**Created:** [April 12, 2013, 8:54pm UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220 "2013-04-12T20:54:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgele](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cgele/32/3453_2.png) [@cgele](https://forum.inductiveautomation.com/u/cgele)\
**Post date:** [April 12, 2013, 8:54pm UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220/1 "2013-04-12T20:54:01Z")

</div>

Is there a function that puts (writes) a bit into an integer?

Thanks

---

<div class="post-metadata">

**Author:** ![CPowell](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CPowell](https://forum.inductiveautomation.com/u/CPowell)\
**Post date:** [April 12, 2013, 10:01pm UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220/2 "2013-04-12T22:01:08Z")

</div>

Not sure where you are doing this, but you could just add them by converting the bit to an integer…  
if by bit you mean binary…  
int(‘bit’, 2)  
ie: int(‘00000101’, 2)

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cgele](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cgele/32/3453_2.png) [@cgele](https://forum.inductiveautomation.com/u/cgele)\
**Post date:** [April 12, 2013, 10:10pm UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220/3 "2013-04-12T22:10:58Z")

</div>

Here is some additional information as to why I’m looking for this function.

I have 5 pumps at a water pump station that I need to start and stop (10 bits or binary values altogether). I want to write these bits to the local controller as a integer. The rest of the information that I’m reading and writing to the local controller are integers. This makes my communications easier to work with on the Master PLC.

---

<div class="post-metadata">

**Author:** ![CPowell](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CPowell](https://forum.inductiveautomation.com/u/CPowell)\
**Post date:** [April 13, 2013, 4:30am UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220/4 "2013-04-13T04:30:38Z")

</div>

Ok, then use Bit Maniputaion Operators.

I created a property to PumpNumber on the screen since you had 5 pumps.  
This code assumes a couple things:

1. This is the command and not the actual status.
2. stop bit is first, start is second.

Put this on event handlers, action -\> actionPerformed.

```auto
#Get Variables
pump = event.source.parent.PumpNumber
tempcommand = system.tag.read("Test/Pump_Command")
command = tempcommand.value

#Lets toggle the correct bits per pump.
if pump == 1:
	newcommand = command^3 #Toggle both start / stop bits for Pump 1
elif pump == 2:
	newcommand = command^12 #Toggle both start / stop bits for Pump 2
elif pump == 3:
	newcommand = command^48 #Toggle both start / stop bits for Pump 3
elif pump == 4:
	newcommand = command^192 #Toggle both start / stop bits for Pump 4
elif pump == 5:
	newcommand = command^768 #Toggle both start / stop bits for Pump 5
else:
	newcommand = command
	system.gui.errorBox("No pump number selected")

#Lets write the new command.
system.tag.writeToTag('Test/Pump_Command', newcommand)

```

Since that code is too simple and I like checks…  
I would use this code:  
I did this as 1 button. This could be changed to two button system.

```auto
#Get Variables
pump = event.source.parent.PumpNumber
tempcommand = system.tag.read("Test/Pump_Command")
command = tempcommand.value

#This prevents errors if someone hits start/stop twice.
newcommand = command

#Lets map the bits per pump. (Start button)
if pump == 1:
	checkstop = command&1
	checkstart = command&2
	if checkstop == 1 and checkstart == 2: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^1
	elif checkstop != 1 and checkstart != 2: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^2
	else:
		newcommand = command^3 #Toggle both Start / Stop bits.
elif pump == 2:
	checkstop = command&4
	checkstart = command&8
	if checkstop == 4 and checkstart == 8: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^4
	elif checkstop != 4 and checkstart != 8: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^8
	else:
		newcommand = command^12 #Toggle both Start / Stop bits.
elif pump == 3:
	checkstop = command&16
	checkstart = command&32
	if checkstop == 16 and checkstart == 32: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^16
	elif checkstop != 16 and checkstart != 32: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^32
	else:
		newcommand = command^48 #Toggle both Start / Stop bits.
elif pump == 4:
	checkstop = command&64
	checkstart = command&128
	if checkstop == 64 and checkstart == 128: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^64
	elif checkstop != 64 and checkstart != 128: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^128
	else:
		newcommand = command^192 #Toggle both Start / Stop bits.
elif pump == 5:
	checkstop = command&256
	checkstart = command&512
	if checkstop == 256 and checkstart == 512: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^256
	elif checkstop != 256 and checkstart != 512: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^512
	else:
		newcommand = command^768 #Toggle both Start / Stop bits.
else:
	newcommand = command
	system.gui.errorBox("No pump number selected")

#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)

```

I hope I explained what I am doing. Let me know if you have any questions.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![CPowell](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CPowell](https://forum.inductiveautomation.com/u/CPowell)\
**Post date:** [April 16, 2013, 1:23pm UTC](https://forum.inductiveautomation.com/t/getbit-opposite-function/6220/5 "2013-04-16T13:23:22Z")

</div>

Here is some cleaner code:

Start button:

```auto
#Get Variables
pump = event.source.parent.PumpNumber
command = system.tag.read("Test/Pump_Command").value

#Figure out the integer equivalent to the start / stop bits.
stop = 2**((pump*2)-2)
start = 2**((pump*2)-1)

#This prevents errors if someone hits start/stop twice.
newcommand = command

#Check if a pump is selected.
if pump > 0:
	#Lets map the bits per pump. (Start button)
	checkstop = command&stop
	checkstart = command&start
	if checkstop == stop and checkstart == start: #Pump is commanded to stop and start, toggle stop bit 
		newcommand = command^stop
	elif checkstop != stop and checkstart != start: #Pump is not commanded to start nor stop, toggle start bit
		newcommand = command^start
	elif checkstop == stop and checkstart != start: #Pump is commanded to stop and not start. toggle both bits
		newcommand = command^(stop+start)
else:
	system.gui.errorBox("No pump number selected")

#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)
```

Stop Button:

```auto
#Get Variables
pump = event.source.parent.PumpNumber
command = system.tag.read("Test/Pump_Command").value

#Figure out the integer equivalent to the start / stop bits.
stop = 2**((pump*2)-2)
start = 2**((pump*2)-1)

#This prevents errors if someone hits start/stop twice.
newcommand = command

#Check if a pump is selected.
if pump > 0:
	#Lets map the bits per pump. (Start button)
	checkstop = command&stop
	checkstart = command&start
	if checkstop == stop and checkstart == start: #Pump is commanded to stop and start, toggle start bit 
		newcommand = command^start
	elif checkstop != stop and checkstart != start: #Pump is not commanded to start nor stop, toggle stop bit
		newcommand = command^stop
	elif checkstop != stop and checkstart == start: #Pump is commanded to start and not stop. toggle both bits
		newcommand = command^(stop+start)
else:
	system.gui.errorBox("No pump number selected")

#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)
```

Cheers,  
Chris
