|
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.
Some integer math operations.
Literals in loops
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.
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. 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.
Converting literal strings into numeric values.
Joining string literals
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.
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.
This code creates the table above.
|
Related Info: | Bin$ | Constant | Dim | Hex$ | Loops | Memory | NamingConventions | Operators | ProgramLayout | Types : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |