Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the problem of computer bits from the perspective of Go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the problems about computer bits from the perspective of Go language". In daily operation, I believe many people have doubts about how to understand the problems about computer bits from the perspective of Go language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions about how to understand computer bits from the perspective of Go language. Next, please follow the editor to study!

A piece of code

This time, take the Go language as an example. Go is a C-like language, and some of the underlying layers are still very similar!

Code

Package main import ("fmt"unsafe") func main () {/ / define a character a var a ='a'/ / define a positive integer 3 var b uint8 = 3 var c uint8 = 98 fmt.Printf ("value:% c, decimal:% d, type:% T, binary:% b, size% v bytes\ n", a, a Unsafe.Sizeof (a)) / / 4-byte fmt.Printf ("value% d, decimal:% d, type:% T, binary% b, size% v bytes\ n", b, unsafe.Sizeof (b)) / one byte fmt.Printf ("value% c, decimal,% d, type:% T, binary% b, size% v bytes\ n", c, c Unsafe.Sizeof (c)) / / one byte}

Execution result

There are a few questions

When my a variable is named, why is the character an in decimal 97 and binary 1100001?

Why is the variable c named 98 but can output b?

Bits and bytes

If we want to understand the above-mentioned problems, we still have to understand the essential problems.

Our program, after all, runs in memory.

And our memory stick, it looks something like this.

The essence of the memory strip is the electronic components one by one. After all, there are only two states, power on (1) and no power on (0).

Bit

An electronic component is a bit.

Byte

And one byte is equal to 8 bits, and 1 byte = 8 bits.

One bit, which is a 0 or 1, is binary, and it is either 0 or 1.

One byte is eight zeros or ones, like this, 00000000, if you see less than eight zeros or ones, fill the front to zero, make up for eight bits.

In general, languages generally operate only to bytes and rarely to the right place.

Why is a 97

Although we know above, a bit represents an electronic component with or without power on.

A byte represents a combination of eight electronic components that are powered on or not.

But this does not solve the practical problem, ah, I want to save a 10, add a 20 to calculate, what to do??

So at this time, there must be a rule, which is bright, or which is not, it means what it is.

So there is the ASCII specification, which has the smallest unit of bytes, that is, managing eight zeros or ones at the same time.

For example, the first byte is the first eight digits. If all of them are zeros, it means the decimal number 0.

The eight binary representations are 00000000.

It is also stipulated that, calculated from the end, if the end lights up, the other seven are not lit, which means decimal 1.

00000001

Wait, wait, wait.

Symbol or number corresponding to the specific binary: https://baike.baidu.com/item/ASCII/309296?fromtitle=ASCII%E7%BC%96%E7%A0%81&fromid=3712529&fr=aladdin

You can know it by querying ASCII.

The binary of the letter an is 0110 0001, the decimal system is 97, and the symbol is a.

So it matches the beginning!

Why 98 can output b is still because of ASCII, because 98 represents the letter b, which is binary 0110 0010.

It's just that the output mode is different.

Current coding direction

In fact, a byte, 8 bits, if all the lights, it is 11111111, its decimal system is 255.theoretically speaking, it can support 255symbols.

English-speaking countries should make do, a letter of 8 bits, a byte, a hello is 5 bytes, a total of 40 bits is enough.

But now, the computer has long become a towering tree, China re-use, small Japan re-use, stick re-use, the words of all countries are no longer as simple as 255.

So some codes, such as Chinese GBK, are derived, and all kinds of codes are based on ASCII expansion.

ASCII occupies one byte, 8 bits, so my GBK is not enough, tens of thousands of Chinese characters, then I account for two bytes, 16 bits, 16 zeros or 1, should make do, three bytes, 24 zeros or 1, three bytes decimal system has reached 16777215, tens of millions, enough to preserve the symbols and text of various countries.

However, GBK and other codes are not common, so now we have derived codes such as utf-8 to include codes from various countries.

At present, utf-8 is one of the best coding, basically already supported by all computers.

At this point, the study on "how to understand the problem about computer bits from the perspective of Go language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report