(go) Binary related basics

Source: Internet
Author: User
Tags bitwise operators

Reprint: https://mp.weixin.qq.com/s?__biz=MzU2NjIzNDk5NQ==&mid=2247483797&idx=1&sn= 18579f6a4e319b6adefc02520a59e2bc&scene=21#wechat_redirect

Basic data type of Golang-integer type

Kinds
Signed (minus sign)
1. int8 int16 int32 Int64

unsigned (unsigned)
1. Uint8 uint16 UInt32 UInt64

Schema-specific (depending on the number of system bits)
1. int UINT

Type aliases
1. Unicode character rune type equivalence int32
2. Byte equivalent uint8

Special types
1. UIntPtr, unsigned integral type,
2. Determined by the system to occupy bit size, enough to hold the pointer, and C library or system interface interaction

Range of values

Specific Type range of Values the number of occupied positions
int8 128 to 127 8
Uint8 0 to 255 8
Int16 32768 to 32767 16
UInt16 0 to 65535 16
Int32 2147483648 to 2147483647 32
UInt32 0 to 4294967295 32
Int64 9223372036854775808 to 9223372036854775807 64
UInt64 0 to 18446744073709551615 64

Note: 1 bytes = 8 bits (1 byte = 8bit)

Representation of integers

    • The original code: the first bit is the sign bit (0 indicates a positive number, and 1 indicates a negative number).

    • Anti-code: the symbol bit fixed, the original code to take the reverse.

    • Negative complement: Sign bit fixed, anti-code plus 1.

    • Positive complement: Same as the original code.

Note: The benefits of complement:
* The use of complement can be without any ambiguity of the expression 0.
* Complement can be a good part of the binary operation, complement the symbolic bit participation in the operation, so much easier.

Floating point Type

Mainly in order to represent decimals
can also subdivide float32 and float64 two kinds
Float64 provides higher accuracy than float32

Range of values

number
type Maximum Value minimum non-negative
Float32 3.402823466385288598117041834516925440e+38 1.401298464324817070923729583289916131280e-45
Float64 1.797693134862315708145274237317043567981e+308 4.940656458412465441765687928682213723651e-324

IEEE745 single-precision floating-point format consists of 32 bits, consisting of three constituent fields: 23-bit fractional f,8 bit-biased exponent e,1 bit sign S. These fields are kept in a 32-bit word and encoded. where 0:22 bits contain 23 bits of fractional F; 23:30 bits contain 8-bit exponent e; the 31st bit contains the symbol s.


Image.png

A real number V can be expressed in the form of v= ( -1) sxmx2e in the IEEE 754 standard, as described below:

    • The symbol s (sign) determines whether the real number is positive (s=0) or negative (S=1), and the symbolic bit of the value 0 is handled specially.
    • A valid number M (significand) is a binary decimal, and the range of M is 1≤m<2 or 0≤m<1.
    • Exponent E (exponent) is a power of 2, and its function is to weighting floating-point numbers.
Decimal
sign Bit decimal pointdigits
1 guests 8 Guests 23 Guests

For example, the value of 11000001000100000000000000000000 single-precision floating point is calculated based on IEEE745.

Solving:

1 10000010 00100000000000000000000
Sign bit Index Mantissa because the exponent is not all 0, so the decimal place additional 1
1 10000010 1.00100000000000000000000
-1 2^ (130-127) (2^0 + 2^-3)

Conclusion:-1 * (2^0 + 2^-3) * 2^ (130-127) =-9

Also, you can verify that the binary form of the decimal floating-point 0.1 is correct, and you will find that 0.1 cannot be represented as a finite bits, so the representation in memory is the result of rounding (rounding), i.e. 0X3DCCCCCD, decimal 0.100000001, The error 0.000000001 is thus generated.

The concept of the binary system

We commonly use binary, octal, decimal, and hexadecimal, and decimal is the most important form of expression.

Binary is 0 and 1, octal is 0-7, decimal is 0-9, hexadecimal is 0-9+a-f (uppercase and lowercase).

Bitwise operators

Bitwise AND (&)

Two bits are all 1, the result is 1:

0&0=0;
0&1=1;
1&0=0;
1&1=1;

Usage:

    • Clear 0: If you want a unit to clear zero, then make it all binary to 0, as long as with a everyone is zero value to want with, the result is zero.

    • Take a number middle finger positioning: Find a number, corresponding to the x to take the bit, the corresponding bit of the number is 1, the remaining bits are zero, this number and X for "and operations" can be obtained in the X of the finger positioning.

For example: Set x=1010 1110, take x low 4 bits, with x & 0000 1111 = 0000 1110 can be obtained.

Bitwise OR (|)

As long as there is a 1, the result is 1:

0|0=0;
0|1=1;
1|0=1;
1|1=1;

usage: often comes to a certain location of a data 1; Find a number, corresponding to the x to set 1 bits, the corresponding bit of the number is 1, the remaining bits are zero. This number is in X-phase or can make some position in X 1.

For example: Place the low four position of the x=1010 0000 1, with x | 0000 1111 = 1010 1111 can be obtained.

Xor operation (^)

Two the corresponding bit is "XOR" (the value is different), then the bit result is 1, otherwise 0:

0^0=0;
0^1=1;
1^0=1;
1^1=0;

Usage:

    • Make a specific bit flip: Find a number, corresponding to the X to flip, the corresponding bit of the number is 1, the rest of the bits are zero, this number and x corresponding bit is different or can be obtained; for example: x=1010 1110, make x low 4 bit flip, with x ^ 0000 1111 = 1010 0001 can be obtained

    • Unlike 0 or, keep the original value for example: X ^ 0000 0000 = 1010 1110

    • Two variables exchange the value of the method: 1, the use of the third variable to achieve: c=a; A=b; B=c; 2, the use of addition and subtraction to achieve the exchange of two variables: a=a+b; B=a-b; A=a-b; 3, using a bitwise XOR OR operation to achieve: the use of a number XOR or itself equals 0 and XOR operation in accordance with the Exchange law for example: a = a ^ B; b = A ^ b; A = a ^ B;

Take inverse operation (~)

For a binary number bitwise reversed, will be 0 to change to 0: ~1=0; ~0=1;

Left shift operation (<<)

    • Shift all bits of an operand to the left of a number of bits (left binary discard, right 0) 2<<1 = 4:10 <<1 =100=4

    • If the left-hand side does not include 1, then each left-shift is equal to the number multiplied by 2. -14 (binary: 1111 0010) << 2 = (1100 1000) (High level includes 1, non-conforming rules)

Right shift operation (>>) (<<)

All bits of a number are shifted to the right by a number of digits, positive left 0, negative left 1, and right discard. The operand is shifted one bit to the right, which is equal to the number divided by 2.

Left complement 0 or 1 depends on whether the number of shifts is positive or negative.
Example: 4 >> 2 = 1
Example: -14 (1111 0010) >> 2 = 4 (1111 1100)

Unsigned Right shift operation (>>>)

Shifts the individual bits to the right by the specified number of digits. The left prominent bit is filled with zero after the right shift. Move out the right bit is discarded
Shifts the individual bits to the right by the specified number of digits. The left prominent bit is filled with zero after the right shift. Move out the right bit is discarded
Example: -14>>>2
-14 (1111 1111 1111 1111 1111 1111 1111 0010) >>> 2
= (0011 1111 1111 1111 1111 1111 1111 1100)
= 1073741820

Description

  • The 0x80000000 is the hexadecimal representation of the number, which is converted to binary representation as 10000000000000000000000000000000
  • The priority of the operation, the shift operation is higher than the logical operation,>>> higher than &
  • Bit logic and Operation 1&1 = 1, 0&1 = 0
  • ' >>> ' unsigned Right shift, move out part discard, left bit to fill 0;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.