Showing posts with label binary numbers. Show all posts
Showing posts with label binary numbers. Show all posts

Sunday, September 10, 2023

TI-84 Plus CE Python and TI-83 Premium CE Python Edition: Blinking Binary Strings

TI-84 Plus CE Python and TI-83 Premium CE Python Edition:  Blinking Binary Strings




Introduction


The script BINBLANK.py draws a circle's whose color changes based on a list of binary numbers:  green for 1, yellow for 0.  


The code listed is made for the TI-84 Plus CE Python and TI-83 Premium CE Python Edition.  



Program Script: BINBLANK.py



# ews 2023-06-24

# blink of lights: binary bits


from random import *

from ti_system import *

from ti_draw import *



# set subroutines

def black():

  from ti_draw import *

  set_color(0,0,0)

def white():

  from ti_draw import *

  set_color(255,255,255)

def yellow():

  from ti_draw import *

  set_color(255,255,0)

def lime():

  from ti_draw import *

  set_color(0,255,0)

def dcircle():

  from ti_draw import *

  fill_circle(150,100,50)


# using pixels, not coordinates

# initialization

blist=[]

print("Binary String Lighting")

print("Choose 1 or 2:")

print("1. generate random string")

print("2. enter a bit list")


# key press

k=0

# flag

f=0

while f==0:

  k=wait_key()

  if k==143:

    # 1 key

    n=int(input("Number of bits? "))

    blist=[randint(0,1) for i in range(n)]

    f=1

  if k==144:

    # 2 key

    blist=eval(input("List of bits: "))

    # data check

    m=len(blist)

    for i in range(m):

      if blist[i]!=0 and blist[i]!=1:

        1/0

        # force an error and terminate

    f=1


# draw the light

clear()

black()

fill_rect(0,0,319,209)

white()

dcircle()


# loop

for i in range(len(blist)):

  sleep(.1)

  if blist[i]==0:yellow()

  if blist[i]==1:lime()

  dcircle()

  black()

  sleep(.55)

  # reset

  white()

  dcircle()


# end indicator

black()

dcircle()

white()

# no line breaks in draw_text 

draw_text(50,50,"THE END: PRESS CLEAR")

show_draw()



Download the file:  https://drive.google.com/file/d/16nxEe51KEhTIX3p1E0uAzHUxCvElQTWE/view?usp=sharing


Notes:  


Three modules are used:  random, ti_system, and ti_draw.  The modules ti_system and ti_draw are Texas Instruments-specific modules.


The ti_system module allows for a get key type of function called wait_key().   wait_key() stops execution and waits for the user to press a key.   The value of the key is returned.   There are separate key combinations with [ 2nd ] or [ alpha ].  


The [ 1 ] key returns a value of 143.

The [ 2 ] key returns a value of 144. 


Here are some other key values:


[ 3 ]   145

[ 4 ]   146

[ 5 ]   147

[ 6 ]   148

[ 7 ]   149

[ 8 ]   150

[ 9 ]   151

[ 0 ]   142

[ enter ]   5

[ ← ]   2

[ → ]   1

[  ↑  ]  3

[  ↓  ]  4

[ clear ]  9



The script also checks self-entered lists to see that each entry is a 0 or 1.   If not, the program "calculates" 1 ÷ 0 to cause a program-stopping error.  


Eddie



All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, July 22, 2023

HP Prime and TI-84 Plus CE: Binary Dot Product

HP Prime and TI-84 Plus CE:   Binary Dot Product



Binary Dot Product



The binary, or inner product, of two binary integers is calculated by:


1.  Using the AND operator on each bit.

2.  Count the number of 1 bits from the result.

3.  Take the result mod 2.   The result will either by 0 (an even number of 1 bits) or 1 (an odd number of 1 bits).


Examples:


Binary dot product of 111011 and 100110.


111011

100110


From the left:

1st bit:  1 and 1 = 1

2nd bit:  1 and 0 = 0

3rd bit:  1 and 0 = 0

4th bit: 0 and 1 = 1

5th bit:  1 and 1 = 1

6th bit: 1 and 0 = 0


Number of 1 bits:  2


2 mod 2 = 0


Result:  111011 • 100110 = 0



Binary dot products are used in various applications, especially in quantum mechanics.  Specific examples include the Bernstein-Vazirani Algorithm.  




Program Notes



In order to compare the individual bits, the two binary integers are converted to strings.   A loop is executed to compare string characters one by one.  


The HP Prime has the BITAND function which compares each pairs of bits with the AND operation.  





HP Prime Program:  BINDOT



EXPORT DOTBIN(x,a)

BEGIN

// EWS 2023-05-13

// Bernstein-Vazirani

// x, a: binary integers

LOCAL f,m,i,s,c;


f:=BITAND(x,a);

// convert to strings

c:=CEILING(LOG(f)/LOG(2));

s:=STRING(f);


FOR i FROM 2 TO c+1 DO

IF MID(s,i,1)=="1" THEN

m:=m+1;

END;

END;


m:=m MOD 2;  

RETURN m;

END;



TI-84 Plus CE Program:  DOTBIN



"EWS 2023-05-11"

Disp "BINARY DOT PRODUCT"

Input "BINARY 1? ",Str1

Input "BINARY 2? ",Str2

length(Str1) → A

length(Str2) → B


If A ≠ B

Then

If A < B

Then

For(I, 1, B - A)

"0" + Str1 → Str1

End

Else

For(I, 1, A - B)

"0" + Str2 → Str2

End

End

End


ClrHome

Disp Str1, Str2

Pause


0 → M


For(I, 1, max(A,B))

If sub(Str1, I, 1)="1" and sub(Str2, I, 1)="1"

Then

M + 1 → M

End

End


remainder(M,2) → M

Disp M




Examples


Example 1:  1101 • 1001001 = 1


Example 2:  11100 • 11110 = 1


Example 3:  1011110 • 10101 = 0



Source

 

Biswas, Shrey.  "The Bernstein-Vazirani Algorithm: Quantum Algorithms Untangled"  Quantum Untangled.  Medium.  February 4, 2021.   Retrieved May 12, 2023.  https://medium.com/quantum-untangled/the-bernstein-vazirani-algorithm-quantum-algorithms-untangled-67e58d4a5096





Eddie


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sunday, May 29, 2022

HP 42S/DM42: Base Programs

 HP 42S/DM42:   Base Programs


Unsigned NOT


The program UNOT applies a NOT to a binary integer (flips zeros to ones and ones to zeros) using unsigned binary integers.  The number of bits (size) is prompted.


HP 42S/DM42 Program UNOT


00  {40-Byte Prgm}

01  LBL "UNOT"

02  BINM

03  "BIN?"

04  PROMPT

05  STO 01

06  EXITALL

07  "SIZE?"

08  PROMPT

09  STO 02

10  2

11  X<>Y

12  Y↑X

13  RCL- 01

14  STO 02

15  1

16  -  

17  BINM

18  END


Examples:


Unsigned NOT of 10100, size 5:  1011  (01011)


Unsigned NOT of 10100, size 8:  11101011


Shift Left


Makes a binary integer shift left: a zero is added to the right side of the integer and "drops" off the left most digit.  The number of bits (size) is prompted.  The binary number is assumed to be non-negative. 


HP 42S/DM42 Program SL16


00  {35-Byte Prgm}

01  LBL "SL16"

02  BINM

03  "BIN?"

04  PROMPT

05  EXITALL

06  2

07  ×

08  "SIZE?"

09  PROMPT

10  2

11  X<>Y

12  Y↑X

13  MOD

14  BINM

15  END


Examples:


Shift Left:  10100, size 5:  1000   (01000)


Shift Left:  10100, size 8:  101000  (00101000)


Shift Right (Logical)


Makes a binary integer shift right: a zero is added to the left side of the integer and "drops" off the left most digit.  The binary number is assumed to be non-negative. A logical shift right divides an integer by 2 and taking the integer result.


HP 42S/DM42 Program SR16


00  {24-Byte Prgm}

01  LBL "SR16"

02  BINM

03  "BIN?"

04  PROMPT

05  EXITALL

06  2

07  ÷

08  IP

09  BINM

10  END


Examples:


Shift Right:  10100:  1010


Shift Right:  1010:  101


The next two programs deals with RGB and HEX codes for computer colors.


HP 42S/DM42 Program CLR→:   RBG to Hexadecimal Code


00  {51-Byte Prgm}

01  LBL "CLR→"

02  DECM

03  "RED?"

04  PROMPT

05  65536

06  BASE×

07  "GREEN?"

08  PROMPT

09  256

10  BASE×

11  BASE+

12  "BLUE?"

13  PROMPT

14  BASE+

15  HEXM

16  END


Example:

Red: 221, Green: 80, Blue 109

Result:  HEX Code:  DD506D


HP 42S/DM42 Program  →CLR:   Hexadecimal to RGB Code


00  {63-Byte Prgm}

01  LBL "→CLR"

02  HEXM

03  "HEX CODE?"

04  PROMPT

05  STO 00

06  DECM

07  65536

08  BASE÷

09  STOP   // Red

10  65536

11  BASE×

12  RCL 00

13  X<>Y

14  BASE-

15  STO 00

16  256

17  BASE÷

18  STOP   // Green

19  256

20  BASE×

21  RCL 00

22  X<>Y

23  BASE-

24  END


Example:  

HEX Code:  103E22

Result:  Red:  16, Green:  62,  Blue:  34


Eddie



All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sunday, August 2, 2020

HP Prime and Casio fx-CG 50: Binary Conversions

HP Prime and Casio fx-CG 50:   Binary Conversions 

x = 2 * (1 + n)^p

The program DECOM2 takes a real number, x, and decomposes it to the form of:

x = 2 * (1 + n)^p

HP Prime Program:  DECOMP2

EXPORT DECOMP2(x)
BEGIN
// 2020-07-11 EWS
// x → 2^p*(1+n)
LOCAL s,p,n,str;
s:=ABS(x);
p:=FLOOR(LOG(s)/LOG(2));
n:=s/2^p-1;
str:=STRING(SIGN(X))+"*2^"+STRING(p)+
"*(1+"+STRING(n)+")";
RETURN str;
END;

Casio fx-CG 50 Program:  DECOMP2
188 bytes

"2020-07-11 EWS"
"X"? → X
Abs X → S
Int (log S ÷ log 2) → P
S < 1 ⇒ P - 1 → P 
S ÷ 2^P - 1 → N
X ÷ S → T
ClrText
Locate 1, 3, X
Locate 18, 3, "="
Locate 1, 5, T
Locate 4, 5, "×2^"
Locate 9, 5, P
Locate 18, 5, "×"
Locate 1, 6, "(1+"
Locate 5, 6, N
Locate 18, 6, ")"

Examples:

-720 = -1 * 2^9 * (1 + 0.40625)

0.7868 = 1 * 2^-1 * (1 + 0.5736)

Converting Numbers to Binary

The program RBIN converts any real number in base 10 to base 2, including non-integers and negative numbers.  The result is displayed as a string.   The HP Prime program has one string for output while the Casio fx-CG 50 uses two:  Str1 for the integer part and Str2 for the fractional part.

RBIN will need two arguments:  the number to be converted and the precision level.

For the precision level:

1 rounds the number to the nearest 1/2 before conversion.

2 rounds the number to the nearest 1/4 before conversion.

3 rounds the number to the nearest 1/8 before conversion.

4 rounds the number to the nearest 1/16 before conversion.

and so on.

HP Prime Program:  RBIN

EXPORT RBIN(x,p)
BEGIN
// 2020-07-11 EWS
// Decimal to Binary
// decimal, precision
// for real x, rounded 1/2^p

LOCAL b,i,f,n,s,w,k,str;
s:=ABS(x);
b:=IP(s)+ROUND(FP(s)*2^p,0)/2^p;
str:="";
i:=IP(b);
f:=FP(b);

IF i≠0 THEN
n:=IP(LOG(i)/LOG(2));
FOR k FROM n DOWNTO 0 STEP 1 DO
w:=IP(i/2^k);
str:=str+STRING(w);
i:=i-w*2^k; 
END;
END;

IF f≠0 THEN
str:=str+".";
FOR k FROM 0 TO p-1 DO
w:=IP(2*f);
str:=str+STRING(w);
f:=FP(2*f);
END;
END;

IF x<0 font="" then="">
str:="-"+str;
END;

RETURN str;
END;

Casio fx-CG 50:  RBIN
372 bytes

"2020-07-11 EWS"
"DEC>BIN: NUM"? → X
"PRECISION"? → P

Abs X → S
Int S + RndFix(Frac S × 2^P, 0) ÷ 2^P → B
" " → Str 1
Int B → I
Frac B → F

If I ≠ 0
Then
Int (log I ÷ log 2) → N
For N → K To 0 Step -1
Int (I ÷ 2^K) → W
W = 0 ⇒ Str 1 + "0" → Str 1
W = 1 ⇒ Str 1 + "1" → Str 1
I - W × 2^K → I
Next
IfEnd

" " → Str 2
If F ≠ 0
Then
"." → Str 2
For 0 → K To P-1
Int (2F) → W
W = 0 ⇒ Str 2 + "0" → Str 2
W = 1 ⇒ Str 2 + "1" → Str 2
Frac (2F) → F
Next
IfEnd

X < 0 ⇒ "-" + Str 1 → Str 1

ClrText
Locate 1, 3, X
Locate 20, 3, "="
Locate 1, 5, Str 1
Locate 1, 6, Str 2

Examples:

RBIN(-0.985,3) returns "-1"
RBIN(-0.985,6) returns "-.111111"
RBIN(-0.985,9) returns "-.111111000"

RBIN(860.63,3) returns "1101011100.101"
RBIN(860.63,6) returns "1101011100.101000"
RBIN(860.63,9) returns "1101011100.101000011"

Take care,

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Saturday, April 25, 2020

HP 42S/DM42/Free 42: Advanced Boolean Functions

HP 42S/DM42/Free 42: Advanced Boolean Functions

Introduction

The programs NOTBIT, NAND, NOR, and IMPL are four advanced Boolean functions that work on Binary Numbers.  You designate a bit size, up to 35, on each of these functions.   There is no signed bit in these programs.  Results are stored in the Alpha Register while the decimal equivalent is stored in the X register. 

HP 42S/DM42/Free 42 Program:  NOTBIT

00 { 57-Byte Prgm }
01▸LBL "NOTBIT"
02 "BIT SIZE?"
03 PROMPT
04 STO 00
05 BINM
06 "BIN?"
07 PROMPT
08 STO 01
09 DECM
10 2
11 RCL 00
12 Y↑X
13 X<>Y
14 BASE-
15 1
16 BASE-
17 BINM
18 "NOT: "
19 ARCL ST X
20 AVIEW
21 EXITALL
22 .END.

Example:

NOT 11011, bit size:  8

Result: 
NOT:  11100100

HP 42S/DM42/Free 42 Program:  NAND

a NAND b = NOT ( a AND b )

00 { 67-Byte Prgm }
01▸LBL "NAND"
02 "BIT SIZE?"
03 PROMPT
04 STO 00
05 BINM
06 "BIN1?"
07 PROMPT
08 STO 01
09 "BIN2?"
10 PROMPT
11 STO 02
12 AND
13 DECM
14 2
15 RCL 00
16 Y↑X
17 X<>Y
18 BASE-
19 1
20 BASE-
21 BINM
22 "NAND: "
23 ARCL ST X
24 AVIEW
25 EXITALL
26 .END.

Example:

110011 NAND 111100, bit size: 8

Result:
NAND:  11001111

HP 42S/DM42/Free 42 Program:  NOR

a NOR b = NOT ( a OR b )

00 { 64-Byte Prgm }
01▸LBL "NOR"
02 "BIT SIZE"
03 PROMPT
04 STO 00
05 BINM
06 "BIN1?"
07 PROMPT
08 STO 01
09 "BIN2?"
10 PROMPT
11 STO 02
12 OR
13 DECM
14 2
15 RCL 00
16 Y↑X
17 X<>Y
18 BASE-
19 1
20 BASE-
21 BINM
22 "NOR: "
23 ARCL ST X
24 AVIEW
25 EXITALL
26 .END.

Example:

110011 NOR 111100, bit size: 8

Result:
NOR:  11000000

HP 42S/DM42/Free 42 Program:  IMPL

IMPL (Implication):  a → b = (NOT a) OR b

00 { 66-Byte Prgm }
01▸LBL "IMPL"
02 "BIT SIZE?"
03 PROMPT
04 STO 00
05 BINM
06 "BIN1?"
07 PROMPT
08 STO 01
09 DECM
10 2
11 RCL 00
12 Y↑X
13 X<>Y
14 BASE-
15 1
16 BASE-
17 BINM
18 "BIN2?"
19 PROMPT
20 STO 02
21 OR
22 "A→B: "
23 ARCL ST X
24 AVIEW
25 EXITALL
26 .END.

Example:

110011 IMPL 111100, bit size: 8

Result:
IMPL:  11111100

You can download the raw files here: 
https://drive.google.com/open?id=19u8tPcX4_-o0lwvjUc0KO4-WSmhnxpwe

Source:

John W. Harris and Horst Stocker  Handbook of Mathematics and Computation Science  Springer:  New York, NY.  2006.  ISBN 978-0-387-94746-4

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Sunday, March 8, 2020

HP Prime: Advanced Boolean Functions

Advanced Boolean Functions





This is a collection of advanced Boolean functions and how you can do this on a scientific calculator that has base conversions and Boolean functions (and, or, not).   One thing to keep in mind is that Boolean functions on a scientific calculator assume that you are working with a set bit size.

Order of Operations:  Parenthesis, NOT, AND, OR

For the examples below, I am going to work with only 8 bit size binary integers, and display the last 8 bits (rightmost 8 digits in Binary numbers).   For all examples, let (in base 2):

A = 1100 1010
B = 1110 0011

Like the Boolean functions and, or, and not, these functions work on each bit (0s and 1s) of the binary form of the number.

Integer Types on the HP Prime

On the HP Prime, the integer type is used for Boolean function calculations.  Symbolize integer types by preceding it by a hashtag # and designated a letter at the end of the integer:  b for binary, o for octal, d for decimal, and h for hexadecimal.

Example:  #11010b  represents the binary number 11010.

You can specify the bit size, from 1 to 64, by attaching a colon and bit size in between the integer and it's indicator.

Example:  #11010:8b represents the 11010 in an 8-bit format.  You can specify the default size in the Home Settings.

NAND (Not And)

The function NAND is also known as the Shaffer function.

nand(A, B) = not(A and B)

nand(1100 1010, 1110 0011) = not(1100 1010 and 1110 0011) = 0011 1101

Truth Table - NAND
0 nand 0 = 1
0 nand 1 = 1
1 nand 0 = 1
1 nand 1 = 0

HP Prime Program:  NAND

EXPORT NAND(a,b)
BEGIN
// not and Boolean Function
RETURN  NOT (a AND b);
END;

Syntax:  NAND(a,b)

NOR (Not Or)

The function NOR is also known as the Peirce function.

nor(A, B) = not(A or B)

nor(1100 1010, 1110 0011) = not(1100 1010 or 1110 0011) = 0001 0100

Truth Table - NOR
0 nor 0 = 1
0 nor 1 = 0
1 nor 0 = 0
1 nor 1 = 0

HP Prime Program:  NOR

EXPORT NOR(a,b)
BEGIN
// not or Boolean Function
RETURN  NOT (a  OR  b);
END;

Syntax:  NOR(a,b)

XOR (Exclusive Or)

Note:  some calculators will have the XOR function

xor(A, B) = (A or B) and (not A or not B)

xor(1100 1010, 1110 0011)
= (1100 1010 or 1110 0011) and (not 1100 1010 or not 1110 0011)
= 0010 1001

Truth Table - XOR
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

Equivalence (←→), XNOR

A ←→ B = (not A and not B) or (A and B)

1100 1010 ←→ 1110 0011
= 1100 1010 xnor 1110 0011
= (not 1100 1010 and not 1110 0011) or (1100 1010 and 1110 0011)
= 1101 0110

Truth Table - ←→, xnor 
0 xnor 0 = 1
0 xnor 1 = 0
1 xnor 0 = 0
1 xnor 1 = 1

HP Prime Program XNOR

EXPORT XNOR(a,b)
BEGIN
// equivalence, XNOR
RETURN (NOT a AND NOT b) or (a AND b);
END;

Syntax:  XNOR(a, b)

Implication (→)

A → B = (not A) or B

1100 1010 → 1110 0011
= (not 1100 1010) or 1110 0011
= 1111 0111

Truth Table -  Implication, →
0 → 0 = 1
0 → 1 = 1
1 → 0 = 0
1 → 1 = 1

HP Prime Program:  IMPL

EXPORT IMPL(a,b)
BEGIN
// implication
RETURN (NOT a) or b;
END;

Source:

John W. Harris and Horst Stocker.  Handbook of Mathematics and Computation Science  Springer:  New York, NY.  2006.  ISBN 978-0-387-94746-4

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Thursday, May 3, 2012

Logic Operators and the TI-36X Pro

Introduction

This blog entry is in response to a email regarding the logic operations I received from jcroot on the Texas Instruments TI-36X Pro Review: (link here)

Hello Eddie!

How are you today? I was just wondering if you could please give us an example of how to use the "Logic function" on this calculator, I am trying to follow the manual but I still can't understand in which situations this could be useful.


jcroot, I hope that this blog entry (at least in part) answers your question. I had to research a bit because I must confess that I do not use logic operators much on calculators.

Logic Operators

The most common logic operators are: and, or, not, 2's compliment and xor. The TI-36X has two additional operators xnor and nand. The latter two are not common on calculators.

I tend to think binary numbers as the prime example for these logic operators. It is my understanding that the operators compare the binary form (base 2 integers) of an integer and if necessary, convert the answer back into the required form (decimal, hexadecimal, or octal).

Binary Numbers

Binary numbers are numbers consisting of 0 and 1, with each digit being a bit. Often, the bit 0 is referred to "off" or "false", while the bit 1 is referred to "on" or "true". Each bit resembles a power of 2, with powers of 2 increasing from right to left:

...(2^n)...(2^5)(2^4)(2^3)(2^2)(2^1)(2^0)

For example, if the first (rightmost bit) is 1, then the value "1" is turned on. If the second bit is 1, then the value "2" is turned on, the third bit gives a value of "4", fourth bit gives a value of "8", and so on. Binary numbers are often written with a subscript of 2. On calculators, they are often designated with b or a "#b".

On the TI-36X Pro

Change to BINary Mode: Press [mode], arrow down five times, select BIN, hit [enter], then [2nd] [mode] (quit). You can designate DECimal (normal - base 10), HEXadecimal (base 16), and OCTal (base 8) modes in a similar way.

Designate a binary number in any mode: type your binary number, [2nd] [ 9 ] (base n), select TYPE, select 2: b. This procedure is not necessary if you are in Binary Mode.

[2nd] [ 9 ] (base n) gives you access to conversions (CONV), designations (TYPE), and operations (LOGIC).

One Argument Logic Operators

not: This operator basically switches a bit. In binary numbers, if the bit is 0, then not 0 = 1. Similarly, if the bit is 1, then not 1 = 0.

2's Complement: This complement is a way to manipulate binary numbers such that the arithmetic, specifically addition and subtraction of binary numbers, is accurate. More information is found by clicking on this sentence.

Two Argument Logic Operators

The following logic operations answer different types of questions:

and: Are both bit 1 true and bit 2 true?

or (inclusive or): Is either bit 1 or bit 2 true? Bit 1 and bit 2 can both be true.

xor (exclusive or): Is either bit 1 true or bit 2 true - without both bits being true?

xnor (not exclusive or): The opposite of xor. Are both bits true or both bits false?

nand (not and): The opposite of and. Is at least one of the bits false?

The following table summaries and, or, xor, xnor, and nand.

How the TI-36X Works With Binary Numbers

All binary numbers work with 10 bit integers. Attempting to work integers greater than 10 bits causes a SYNTAX Error. This error is not mentioned in the TI-36X Manual.

The binary number of a TI-36X looks like this:

##########

where # is a 0 or 1. If you enter less than 10 bits, the calculator "fills" the leftmost bits with 0. Note that I designated the leftmost bit (10th bit) red and bold. This is known as the sign bit. If this bit is 0, then the integer is positive.

(sign) (2^8) (2^7) (2^6) (2^5) (2^4) (2^3) (2^2) (2^1) (2^0)
0 [ 1 = "on", 0 = "off"]

However, if the leftmost bit (10th bit) is 1, then the integer is negative. I am not 100% sure how the rest of the bits are designated.

Note in all examples, I designate binary numbers with the "b", like the calculator does.

10001b = 0000010001b = 17

1111101111b = -17

For the TI-36X Pro, the boundary for binary integers in the interval [-511, 511].
(2^9 - 1 = 511) Remember, the 10th bit is the sign bit on this calculator.

When using logic operators, the TI-36X compares each bit, including the 10th bit.

By the way, binary integers can be stored and recalled in variables.

Examples:

Let x = 0000011101b (5th, 4th, 3rd, and 1st bits are on, x = 29)
and y = 0000010100b (5th and 3rd bits are on, y = 20)

(decimal equivalents in parenthesis)
x and y = 0000010100b (29 and 20 = 20)
x or y = 0000011101b (29 or 20 = 29)
x xor y = 0000001001b (29 xor 20 = 9)
x xnor y = 1111110110b (29 xnor 20 = -10)
x nand y = 1111101011b (29 nand 20 = -21)

So, when I think of logic operations, I think of comparing binary numbers (or binary equivalent of integers) bit by bit, given a designated amount of bits.

Hope this helps,

Eddie.



This blog is property of Edward Shore. © 2012

Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...