Literals - Learn about Literals

By: Kevin Picone



I N D E X:






What are they?


      Literal constants are values or strings expressed as themselves within a programs source code. PlayBASIC supports three main types of literal constants, they are Integers, Floating Point and Strings.




Integer Literals


      Integer literals are numerical constants, these constants are whole number values ranging between -2147483648 to 2147483647.

      Some examples might be 45 , -9543 & 30000

      We'll most often be using literals to seed the value of variables, math operations and loops.

      Setting the value of an integer variable.

  
; assign the Integer variable A, the literal value of integer 45
  A= 45
  Print A
  Sync
  WaitKey
  



      Some integer math operations.
  
; assign the Integer variable A the result of the operation
; between  literal integer 45 multiplied by literal 123
  A= 45 * 123
  Print A
  Sync
  WaitKey
  




      Literals in loops
  
; Start a For/NEXT to loop from literal 1 through to 10.
  For lp =1 To 10
   ; Print the current value of the loop counter variable
     Print LP
  Next
  Sync
  WaitKey
  


Top





Floating Point Literals


      Floating point literals are numerical constants, these constants are those made up of a whole and decimal part to the number value. These values have a range of approximately 3.4E-38 to 3.4E+38.

      Some examples might be 33.33 , -123.45 & 2342.00321

      Setting the float value of a float + integer variable.

  
; assign the Float Variable A#, the literal value of float 1234.56
  A#= 1234.56
  Print A#
  
; assign the Integer Variable B, the literal value of float 1234.56
  B= 1234.56
  Print B
  
  Sync
  WaitKey
  


      If you run this example, you'll notice that PlayBASIC automatically recasts assignments between Float literals and integer variables (and vice versa) for you. But, one thing to remember is that when this occurs PlayBASIC will auto round. You can use to Int or Floor functions to avoid this.

Top






Character and String Literals


      String literals are textual constants, unlike Integer and Float Literals, String Literals are enclosed between a pair of quote (")symbols. The string literal can be contain any ASC II character, except the quote character (Tip: use asc() or that ) . They are generally used when we need to display information back to the user, which might be the name of our program, a list of high score names or perhaps even usage instructions.

      Some examples might be "Hello World" , "PlayBASIC" & "Press Space To Start"

      Copying the a String literal into a string variable.
  
; assign the String variable GAMENAME$, the literal string
  GameName$="Asteroid 10000"
  Print GameName$
  Sync
  WaitKey
  


      Converting literal strings into numeric values.
  
  
  ScoreText$="10000"
  
  Print ScoreText$
  
; Use the VAL function to convert this string
; into an integer value
  Print Val(ScoreText$)
  
; Use the VAL# function to convert this string
; into an float value
  Print Val#(ScoreText$)
  
  Sync
  WaitKey
  
  
  



      Joining string literals
  
  ThisWord$="Hello"
  Print ThisWord$+" World"
  
  For lp=1 To 10
   ; Join the string literal with a string
   ;  representation of the loop counter
     Print "Count="+Str$(lp)
  Next
  
  Sync
  WaitKey
  



Top








Binary and Hexadecimal Literals


      Integer numbers can alternatively be represented in our source code directly as be Binary or Hexadecimal format. These are commonly used in computer programming as internally your computers processor and memory used binary. Binary numbers are represented with a % symbol and $ sign indicates a hexadecimal number.

      Unlike decimal numbers which are base ten, Binary is a base two number system. Where each digit in a binary number is limited to one of 2 states, it's either 0 or 1. A single binary digit can is also called a bit. Since a single bit can only represent a numeric range of 0 to 1, then we'll need more bits to represents bigger numbers. Just like decimal numbers though the digits in a binary number are presented the highest to lowest order.

      In decimal numbering system each digit has ten possible values, ranging from 0 through to 9. So higher order digits represent a multiples of ten, so a four digit decimal number of 2345 is the equivalent of a 2*1000 + 3*100 + 4 *10 + 5. In binary numbers the higher order digits are multiples of two. So four digit binary value of %1111 is the equivalent of 1 * 8 + 1 * 4 + 1 *2 + 1, which is 15 in decimal. The decimal number 2345 in binary would be %100100101001


      4 bit binary examples.

  
  Binary=%0000     Decimal=0
  Binary=%0001     Decimal=1
  Binary=%0010     Decimal=2
  Binary=%0011     Decimal=3
  Binary=%0100     Decimal=4
  Binary=%0101     Decimal=5
  Binary=%0110     Decimal=6
  Binary=%0111     Decimal=7
  Binary=%1000     Decimal=8
  Binary=%1001     Decimal=9
  Binary=%1010     Decimal=10
  Binary=%1011     Decimal=11
  Binary=%1100     Decimal=12
  Binary=%1101     Decimal=13
  Binary=%1110     Decimal=14
  Binary=%1111     Decimal=15
  



     One place where we can use binary is to help simplify your code will be in the camera visibility settings, which are bit based. See CaptureVis



Hexadecimal



      Hexadecimal is base 16 number system. This means that each digit in a hexadecimal number has a numeric range of 0 to15. Now since this range includes 2 digit decimal numbers (10 to 15), to avoid confusion the amounts 10 to 15 are represents as alphabet characters A through F. So the A character equals a value 10, B='s 11, C='s 12, D='s 13, E='s 14 and F='s 15.

      Hexadecimal numbers are preceded by the $ symbol . If we take the decimal value 2345 and convert that to hex we get $929, which can calculated as (9*256)+ (2*16) + (9*1). Moreover Hexadecimal and binary easily related to each other. One digit in a hexadecimal number contains the decimal numeric range of 0 to 15, which is 4 bits in a binary number. Two digits in a hex number are the equivalent of 8 bits, or one byte of computer memory. (See Memory) A two digit number in hex can represent a numeric range of 0 to 255 (256 possible values). For example $00 = 0 decimal, $FF = 255 in decimal.

      Hexadecimal is most frequently used in PlayBASIC to represent RGB colour literals. Colours are really just a series of 3 (RGB) or 4 byte (ARGB) values packed into an 32bit integer. Since each pair of hex digits is equal to a byte, then it stands to reason that a four digit hex literal is the same as two bytes. So we can represent an RGB colour (3 bytes) as a six digit hex value.


      Hexadecimal examples.
  
  
  Hex:$0 = Dec:0
  Hex:$1 = Dec:1
  Hex:$2 = Dec:2
  Hex:$3 = Dec:3
  Hex:$4 = Dec:4
  Hex:$5 = Dec:5
  Hex:$6 = Dec:6
  Hex:$7 = Dec:7
  Hex:$8 = Dec:8
  Hex:$9 = Dec:9
  Hex:$A = Dec:10
  Hex:$B = Dec:11
  Hex:$C = Dec:12
  Hex:$D = Dec:13
  Hex:$E = Dec:14
  Hex:$F = Dec:15
  
  
  Dec:8229 = Hex$2025  Working= (2*4096)+(0*256)+(2*16)+(5*1)
  Dec:12817 = Hex$3211  Working= (3*4096)+(2*256)+(1*16)+(1*1)
  Dec:20101 = Hex$4E85  Working= (4*4096)+(14*256)+(8*16)+(5*1)
  Dec:21345 = Hex$5361  Working= (5*4096)+(3*256)+(6*16)+(1*1)
  Dec:23805 = Hex$5CFD  Working= (5*4096)+(12*256)+(15*16)+(13*1)
  Dec:24823 = Hex$60F7  Working= (6*4096)+(0*256)+(15*16)+(7*1)
  Dec:31394 = Hex$7AA2  Working= (7*4096)+(10*256)+(10*16)+(2*1)
  Dec:32659 = Hex$7F93  Working= (7*4096)+(15*256)+(9*16)+(3*1)
  Dec:36555 = Hex$8ECB  Working= (8*4096)+(14*256)+(12*16)+(11*1)
  Dec:40876 = Hex$9FAC  Working= (9*4096)+(15*256)+(10*16)+(12*1)
  Dec:43144 = Hex$A888  Working= (10*4096)+(8*256)+(8*16)+(8*1)
  Dec:43787 = Hex$AB0B  Working= (10*4096)+(11*256)+(0*16)+(11*1)
  Dec:51174 = Hex$C7E6  Working= (12*4096)+(7*256)+(14*16)+(6*1)
  Dec:53051 = Hex$CF3B  Working= (12*4096)+(15*256)+(3*16)+(11*1)
  Dec:63847 = Hex$F967  Working= (15*4096)+(9*256)+(6*16)+(7*1)
  Dec:64317 = Hex$FB3D  Working= (15*4096)+(11*256)+(3*16)+(13*1)
  
  
  





      This code creates the table above.
  
  
  // -----------------------------------------
  // Show the value of each hex digit
  // -----------------------------------------
  HexDigits$="0123456789ABCDEF"
  
  For lp=1 To Len(HexDigits$)
   ; grab this character from digits string
     s$="$"+Mid$(HexDigits$,lp,1)
   ; get the value of this hex digit
     s$="Hex:"+s$+" = Dec:"+Str$(Val(s$))
     Print s$
     
  Next
  
  Print ""
  Print ""
  
  Size=16
  
  Dim Values(Size)
  
  // Store some random 16 bit values in array
  For lp=0 To size-1
     Values(lp)=Rnd($ffff)
  Next
  
  // Sort the values into order
  SortArray Values(),0,Size-1
  
; show them
  For lp=0 To Size-1
     ThisInt= Values(lp)
     
     HexString$=Right$(Hex$(ThisINT),4)
     
     s$ ="Dec:"+Str$(ThisInt)+" = Hex$"+HexString$
     
   ; Show the working
     
     s$+="  Working= "
     
     DigitValue     = $1000
     For HexLp=1 To 4
        s$+="("+Str$(Val("$"+Mid$(HexString$,HexLP)))
        s$+="*"+Str$(DigitValue)+")+"
        DigitValue=DigitValue>> 4
     Next
     
     Print TrimRight$(s$,"+")
  Next
  
  Sync
  WaitKey
  
  
  


Top







 
Related Info: Bin$ | Constant | Dim | Hex$ | Loops | Memory | NamingConventions | Operators | ProgramLayout | Types :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com