开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

PureBasic/SpiderBasicPreference

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
binary.html 11.22 KB
一键复制 编辑 原始数据 按行查看 历史
麦壳饼 提交于 2016年07月26日 10:47 +08:00 . chuci tijiao
<html><head><title>Working with different number bases</title></head>
<body bgcolor="#FFFFFF" link="#207BC3" vlink="#003B83" alink="#003B83">
<font face="Arial" size="2"><p align="center"><b><font size="5">Working with different number bases</font></b></p>
(Note: These examples use the ^ symbol to mean 'raised to the power of' - this is only for convenience in this document,
SpiderBasic does not currently have a power-of operator! Use the SpiderBasic command Pow() from the &quot;Math&quot; Library instead.)
<p><b>Introduction</b></p><blockquote>
A number base is a way of representing numbers, using a certain amount of possible symbols per digit.
The most common you will know is base 10 (a.k.a. decimal), because there are 10 digits used (0 to 9),
and is the one most humans can deal with most easily. <br>
<br>
The purpose of this page is to explain different number bases and how you can work with them. The reason
for this is that computers work in binary (base 2) and there are some cases where it is advantageous to
understand this (for example, when using logical operators, bit masks, etc).
</blockquote>
<p><b>Overview of number bases</b></p><blockquote>
</blockquote>
<p><b>Decimal System</b></p><blockquote>
Think of a decimal number, and then think of it split into columns. We'll take the example number 1234.
Splitting this into columns gives us:
<pre><font face="Courier New, Courier, mono"size="2"> 1 2 3 4
</font></pre>
Now think of what the headings for each column are. We know they are units, tens, hundreds and thousands,
laid out like this:
<pre><font face="Courier New, Courier, mono"size="2"> 1000 100 10 1
1 2 3 4
</font></pre>
We can see that the number 1234 is made up out of
<pre><font face="Courier New, Courier, mono"size="2"> 1*1000=1000
+ 2* 100= 200
+ 3* 10= 30
+ 4* 1= 4
Total =1234
</font></pre>
If we also look at the column headings, you'll see that each time you move one column to the left we
multiply by 10, which just so happens to be the number base. Each time you move one column to the right,
you divide by 10. The headings of the columns can be called the weights, since to get the total number
we need to multiply the digits in each column by the weight. We can express the weights using indices.
For example 10^2 means '10 raised to the power of two' or 1*10*10 (=100). Similarly, 10^4 means
1*10*10*10*10 (=10000). Notice the pattern that whatever the index value is, is how many times we multiply
the number being raised. 10^0 means 1 (since we multiply by 10 no times). Using negative numbers shows
that we need to divide, so for example 10^-2 means 1/10/10 (=0.01). The index values make more sense when
we give each column a number - you'll often see things like 'bit 0' which really means 'binary digit in column 0'.
<pre><font face="Courier New, Courier, mono"size="2"> In this example, ^ means raised to the power of, so 10^2 means 10 raised to the power of 2.
Column number 3 2 1 0
Weight (index type) 10^3 10^2 10^1 10^0
Weight (actual value) 1000 100 10 1
Example number (1234) 1 2 3 4
</font></pre>
A few sections ago we showed how to convert the number 1234 into its decimal equivalent. A bit pointless, since
it was already in decimal but the general method can be shown from it - so this is how to convert from any
number to its decimal value:
<pre><font face="Courier New, Courier, mono"size="2"> B = Value of number base
1) Separate the number in whatever base you have into it's columns. For
example, if we had the value 'abcde' in our fictional number base 'B',
the columns would be: a b c d e
2) Multiply each symbol by the weight for that column (the weight being
calculated by 'B' raised to the power of the column number):
a * B^4 = a * B * B * B * B
b * B^3 = b * B * B * B
c * B^2 = c * B * B
d * B^1 = d * B
e * B^0 = e
3) Calculate the total of all those values. By writing all those values in their
decimal equivalent during the calculations, it becomes far easier to see the
result and do the calculation (if we are converting into decimal).
</font></pre>
Converting in the opposite direction (from decimal to the number base 'B') is done using division
instead of multiplication:
<pre><font face="Courier New, Courier, mono"size="2"> 1) Start with the decimal number you want to convert (e.g. 1234).
2) Divide by the target number base ('B') and keep note of the result and the
remainder.
3) Divide the result of (2) by the target number base ('B') and keep note of
the result and the remainder.
4) Keep dividing like this until you end up with a result of 0.
5) Your number in the target number base is the remainders written in the order
most recently calculated to least recent. For example, your number would be
the remainders of the steps in this order 432.
</font></pre>
More specific examples will be given in the sections about the specific number bases.
</blockquote>
<p><b>Binary System</b></p><blockquote>
Everything in a computer is stored in binary (base 2, so giving symbols of '0' or '1') but
working with binary numbers follows the same rules as decimal. Each symbol in a binary
number is called a bit, short for binary digit. Generally, you will be working with bytes (8-bit),
words (16-bit) or longwords (32-bit) as these are the default sizes of SpiderBasic's built in types.
The weights for a byte are shown:
<pre><font face="Courier New, Courier, mono"size="2"> (^ means 'raised to the power of', number base is 2 for binary)
Bit/column number 7 6 5 4 3 2 1 0
Weight (index) 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Weight (actual value) 128 64 32 16 8 4 2 1
</font></pre>
So, for example, if we had the number 00110011 (Base 2), we could work out the value of it like this:
<pre><font face="Courier New, Courier, mono"size="2"> 0 * 128
+ 0 * 64
+ 1 * 32
+ 1 * 16
+ 0 * 8
+ 0 * 4
+ 1 * 2
+ 1 * 1
= 51
</font></pre>
An example of converting back would be if we wanted to write the value 69 in binary. We would do it like this:
<pre><font face="Courier New, Courier, mono"size="2"> 69 / 2 = 34 r 1 ^
34 / 2 = 17 r 0 /|\
17 / 2 = 8 r 1 |
8 / 2 = 4 r 0 | Read remainders in this direction
4 / 2 = 2 r 0 |
2 / 2 = 1 r 0 |
1 / 2 = 0 r 1 |
(Stop here since the result of the last divide was 0)
Read the remainders backwards to get the value in binary = 1000101
</font></pre>
Another thing to consider when working with binary numbers is the representation of negative numbers.
In everyday use, we would simply put a negative symbol in front of the decimal number.
We cannot do this in binary, but there is a way (after all, SpiderBasic works mainly with signed numbers,
and so must be able to handle negative numbers). This method is called 'twos complement' and apart
from all the good features this method has (and won't be explained here, to save some confusion) the simplest
way to think of it is the weight of the most significant bit (the MSb is the bit number with the highest weight,
in the case of a byte, it would be bit 7) is actually a negative value. So for a two's complement system, the
bit weights are changed to:
<pre><font face="Courier New, Courier, mono"size="2"> (^ means 'raised to the power of', number base is 2 for binary)
Bit/column number 7 6 5 4 3 2 1 0
Weight (index) -2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Weight (actual value) -128 64 32 16 8 4 2 1
</font></pre>
and you would do the conversion from binary to decimal in exactly the same way as above, but using the new set
of weights. So, for example, the number 10000000 (Base 2) is -128, and 10101010 (Base 2) is -86. <br>
<br>
To convert from a positive binary number to a negative binary number and vice versa, you invert all the bits and
then add 1. For example, 00100010 would be made negative by inverting -&gt; 11011101 and adding 1 -&gt; 11011110. <br>
<br>
This makes converting from decimal to binary easier, as you can convert negative decimal numbers as their positive
equivalents (using the method shown above) and then make the binary number negative at the end. <br>
<br>
Binary numbers are written in SpiderBasic with a percent symbol in front of them, and obviously all the bits in the number
must be a '0' or a '1'. For example, you could use the value %110011 in SpiderBasic to mean 51. Note that you do not need
to put in the leading '0's (that number would really be %00110011) but it might help the readability of your source if
you put in the full amount of bits.
</blockquote>
<p><b>Hexadecimal System</b></p><blockquote>
Hexadecimal (for base 16, symbols '0'-'9' then 'A'-'F') is a number base which is most commonly used when dealing with computers,
as it is probably the easiest of the non-base 10 number bases for humans to understand, and you do not end up with
long strings of symbols for your numbers (as you get when working in binary). <br>
<br>
Hexadecimal mathematics follows the same rules as with decimal, although you now have 16 symbols per column until
you require a carry/borrow. Conversion between hexadecimal and decimal follows the same patterns as between binary
and decimal, except the weights are in multiples of 16 and divides are done by 16 instead of 2:
<pre><font face="Courier New, Courier, mono"size="2"> Column number 3 2 1 0
Weight (index) 16^3 16^2 16^1 16^0
Weight (actual value) 4096 256 16 1
</font></pre>
Converting the hexadecimal value BEEF (Base 16) to decimal would be done like this:
<pre><font face="Courier New, Courier, mono"size="2"> B * 4096 = 11 * 4096
+ E * 256 = 14 * 256
+ E * 16 = 14 * 16
+ F * 1 = 15 * 1
= 48879
</font></pre>
And converting the value 666 to hexadecimal would be done like this:
<pre><font face="Courier New, Courier, mono"size="2"> 666 / 16 = 41 r 10 ^
41 / 16 = 2 r 9 /|\ Read digits in this direction, remembering to convert
2 / 16 = 0 r 2 | to hex digits where required
(Stop here, as result is 0)
Hexadecimal value of 666 is 29A
</font></pre>
The really good thing about hexadecimal is that it also allows you to convert to binary very easily. Each hexadecimal
digit represents 4 bits, so to convert between hexadecimal and binary, you just need to convert each hex digit to 4 bits
or every 4 bits to one hex digit (and remembering that 4 is an equal divisor of all the common lengths of binary numbers
in a CPU). For example:
<pre><font face="Courier New, Courier, mono"size="2"> Hex number 5 9 D F 4E
Binary value 0101 1001 1101 1111 01001110
</font></pre>
When using hexadecimal values in SpiderBasic, you put a dollar sign in front of the number, for example $BEEF.
</body></html>
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML
1
https://gitee.com/PureBasic/SpiderBasicPreference.git
git@gitee.com:PureBasic/SpiderBasicPreference.git
PureBasic
SpiderBasicPreference
SpiderBasicPreference
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /