ios-c_day2___ binary Code

Source: Internet
Author: User
Tags float double

2015.1.20

Decimal

0 1 2 3 4 5 6 7 8 9 10 11 12 .... 99 100 101 ...

Carry: "Every ten in one"

Binary (for computers is the most natural language)

0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

"Every 2 in 1"

High level: 1 Low Level 0

5+6

Machine language

0101001010101010

0101001010101001

1010101010100110

Assembly

5+6

MOVL $%-8 (RBP)

MOVL $6%-12 (RBP)

MOVL%-8 (RBP)%eax

Addl%-12 (RBP)%eax

MOVL%eax%esi

The portability is poor

5+6 C99

int a = 5;

int B = 6;

int c = a+b;

Simple, portable, robust

Compiler: C language--> assembly language---binary language

OC Language--C--assembly language----binary language

123 = 1*10^2 + 2*10^1 + 3*10^0

In addition to the 10 reverse to take the remainder

123/10--3

12/10--2

1/10--1

123

Decimal Turn binary

In addition to the 2 reverse to take the remainder

123

123/2--1

61/2--1

30/2--0

15/2--1

7/2--1

3/2--1

1/2--1

0/2--0

0

123 = 0b1111011 = 1 *2^0 + 1*2^1 + 0*2^2 + 1*2^3 + 1*2^4 + 1*2^5 + 1*2^6 = 1 + 2 + 0 + 8 + 16 + 32 + 64 = 123

Represents the binary number in front of the data plus 0b 0b1010101001 0b11111

345 678 987

/*int Main (int argc,const char *argv[])

{

int bin[32]={};

int num;

scanf ("%d", &num);//lowercase keyboard Enter cannot be used

%d integer data formatting placeholders

int i=0;

while (num) {//num = 0

Bin[i]= num%2;

num/=2;//change the value of num

i++;

}

printf ("0b");

for (i=i-1; i>=0; i--) {

printf ("%d", bin[i]);

}

printf ("\ n");

return 0;

}*/

Octal

0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 ...

Every 8 in 1

Octal number in front plus 0 for octet

010

The size of the data does not change due to the representation of the binary

int main (int argc, const char *argv[])

//{

int a = 10;

int b = 0b1010;

int c = 012;

int d = 0xa;

//

printf ("a =%d b =%d c =%d d =%d\n", A, B, C, D);

//

return 0;

//}

Decimal number to octal number

In addition to the 8 reverse to take the remainder

123

123/8---3

15/8---7

1/8---1

0

123 = 0173 = 3*8^0 + 7*8^1 + 1*8^2 = 3 + 56 + 64 = 123

233 156 78

int main (int argc,const char *argv[])

//{

int num;

scanf ("%d", &num);

printf ("0%o\n", num);//%o octal Digit Placeholder

return 0;

//}

octal into binary

"421"

0 1 2 3 4 5 6 7

000 001 010 011 100 101 110 111

0173 = 0b1111011

Replace each of the octal digits with a three-bit binary number

04567 0765 01234

0B100 101 110 111

0B111 110 101

0B001 010 011 100

Binary number turn octal number

0B 011 110 001 101 011 110 000 110

036153606

Starting from the low, each three bits is a group, the high level is less than three bit 0, and the octal number replaces the three bit binary number

Hexadecimal

Every 16 in one

0 1 2 3 4 5 6 7 8 9 a/a b/b/C d/d e/e f/f

10 11 12 13 ..... 1f ... ... ff ... 100 ... 101

Prefix 0x describes a hexadecimal number 0x100

Decimal number to hexadecimal number

In addition to the 16 reverse to take the remainder

123

123/16--B

7/16--7

0

123 = 0x7b = b*16^0 + 7*16^1 = 11+112 = 123;

Hex Turn Binary

Replace each digit hexadecimal number with a 4-bit binary number, respectively

"8421"

0 1 2 3 4 5 6 7 8

0000 0001 0010 0011 0100 0101 0110 0111 1000

9 a b c D E F

1001 1010 1011 1100 1101 1110 1111

0x2345678 = 0b10001101000101011001111000

0xfeabc9876543210 = 0b1111 1110 1010 1011 1100 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000

Binary Turn hex

Starting from the low, each four-bit is a group, the high level is less than four 0, respectively, the hexadecimal number is replaced by the four-bit binary number

0B0101 1101 0110 1010 1111 0001 1110 1100

0x5d6af1ec

For a large number to be converted to binary, excessive in octal or hexadecimal

345678908765

Units in the computer

The smallest unit in the computer is bits 1bit

Eight bits of one byte 1Byte = 8bits

1Kb = 1024Bytes

1Mb = 1024Kb

1Gb = 1024Mb

1Tb = 1024Gb

1Pb = 1024Tb

HDD Manufacturers

500Gb = 500*1000MB = $ * 1000Mb *1000kb = *1000mb *1000kb *1000byte

int main (int argc, const char * argv[]) {

//

//

printf ("Hello world\n");

//

return 0;

//}

Data is stored in the computer form

Memory: Ram ROM

Made up of bits circuits

Data encoding method

Take 8bits as an example char

With the highest bit as the sign bit, the remaining bits represent the size of the data

The highest bit is 1 description is a negative number, the highest bit is 0 description is a positive number

Original code: The highest bit is the sign bit, the remaining bits represent the absolute size of the data

-6 1000 0110

6+5

00000110 6

00000101 5

//-------------------

00001011 = 11

6+ (-5)

0000 0110

1000 0101

//-----------------

1000 1011 (original code) = 11

The original code cannot be subtracted because the sign bit is also involved in the operation

Anti-code: Positive anti-code is its original code, negative anti-code, is on the basis of the original code, the symbol bit unchanged, the rest of the bit counter

7 Original code: 0000 0111 Reverse Code: 0000 0111

-7 Original code: 1000 0111 Reverse code: 1111 1000

-5 Original code: 1000 0101 anti-code; 1111 1010

-9 Original code: 1000 1001 Reverse code: 1111 0110

-12 Original code: 1000 1100 Reverse code: 1111 0011

6+ (-5)

0000 0110

+ 1111 1010

//----------------

0000 0000 (Anti-code) = 0

-5+ (-6)

1111 1010

+ 1111 1001

//-----------------

1111 0011 (anti-code) ==> 1100 ==>-12

Reverse code to the original code: again to seek back code

0 of the expression is not unique

+0 0000 0000

-0 1000 0000

The reason that the inverse code cannot store the data is that the 0 representation is not unique

Complement: A positive complement is its original code negative complement, is on the basis of anti-code plus 1

6:0000 0110 (complement)

-6:1000 0110-1111 1001-1111 1010 (complement)

-5:1000 0101-1111 1010-1111 1011 (complement)

-6 + (-5)

1111 1010

1111 1011

//---------------

1111 0101 (complement)--1010---1000 1011 (original code) = 11

Complement to the original code: positive number unchanged, for negative, sign bit unchanged, the rest of the bit counter plus 1

A complementary way to store data in a computer

-12 Original code: 1000 1100 Anti-code: 1111 0011 Complement: 1111 0100

-34 Original code: 1010 0010 Anti-code: 1101 1101 Complement: 1101 1110

-128 Original code: 11000 0000 Anti-code: 10111 1111 complement: 1000 0000

Rule: 1 followed by 7 0 is the smallest number 1 in the current range represents the sign bit, and the absolute value size

-127 Original code: 1111 1111 Anti-code: 1000 0000 Complement: 1000 0001

-0 Original code: 1000 0000 Anti-code: 1111 1111 complement: 0000 0000

+0 0000 0000

-128 ~ ~ 127

0x80 ~ ~ 0x7f

162 binary Bits

1000 0000 0000 0000 ~ ~ 0111 1111 1111 1111

0x8000 ~ ~ ~ 0X7FFF

Data type

Integral type

have symbols

64-bit Platform

char short int long Long long

1byte 2bytes 4bytes 8bytes 8bytes

32-bit Platform

1byte 2bytes 4bytes 4bytes 8bytes

No sign

unsigned char 0x00 ~ ~ ~ 0xFF

unsigned int 0x00000000 ~ ~ ~ 0xFFFFFFFF

unsigned short 0x0000 ~ ~ ~ 0xFFFF

unsigned long 0x0000000000000000 ~ ~ ~ 0XFFFFFFFFFFFFFFFF

unsigned long 0x0000000000000000 ~ ~ ~ 0XFFFFFFFFFFFFFFFF

/*int Main (int argc, const char *argv[])

{

int A;

printf ("char =%lu\n", sizeof (char)),//sizeof operator for data type, or variable takes up memory space size

Char a= 0x80, b = 0x7f;

printf ("Char%d ~ ~ ~%d\n", A, b);

printf ("Unsigned char =%ld\n", sizeof (unsigned char));

0 ~ 255

unsigned char a= 0x00, b = 0xFF;

printf ("Char%d ~ ~ ~%d\n", A, b);

Short a =0x8000, B = 0x7fff;

printf ("Short%d ~ ~ ~%d\n", A, b);

printf ("short =%lu\n", sizeof (short));

int a = 0x80000000, B = 0x7fffffff;

printf ("int%d ~ ~ ~%d\n", A, b);

printf ("int =%lu\n", sizeof (int));

Long a= 0x8000000000000000, b= 0x7fffffffffffffff;

printf ("Long%ld ~ ~ ~%ld\n", A, b);

printf ("Long =%lu\n", sizeof (long));

printf ("Long long =%lu\n", sizeof (long Long));

return 0;

}

*/

%d:char Short int

%ld:long

%lld:long Long

%u:unsigned char unsigned short unsigned int

%lu:unsigned Long

%llu:unsigned Long Long

%x: Print hex

%o: Print octal

int main (int argc,const char *argv[])

//{

Long Long a=789;

printf ("A =%lld", a);

//

unsigned int b=12;

printf ("%u", b);

//

return 0;

//}

Floating-point types

Single-precision double-precision long double precision

Float Double long Double

4 bytes 8 bytes 16 bytes

int main (int argc, const char *argv[])

//{

printf ("float =%lu\n", sizeof (float));

printf ("double =%lu\n", sizeof (double));

printf ("Long double =%lu\n", sizeof (long double));

//

return 0;

//}

Floating-point type data representation

3.14

Scientific notation: 5678.678 5.678678 * 10^3; (cannot be represented in the computer)

Exponential method: 34.56 ==> 3.456e1

int main (int argc,const char *argv[])

//{

Float F1 = 3.14;//Common way

printf ("%f\n", F1);

float F2 = 3.456e1;//exponential method

printf ("%f\n", F2);

float F3 = 3.45e-2;

printf ("%f\n", F3);

//

return 0;

//}

Floating-point type data is stored in memory form

3.145-0.3145e1

-32.56-->-0.3256e2

1. Sign bit 2. Decimal Place 3. decimal point

Float Precision 6~7 Bit

Double precision 15~16 bit

int main (int argc,const char *argv[])

{

float f1 = 567.2345434532345345;

printf ("%f\n", F1);//%f

Double F2 = 6798.34567876543234567876;

printf ("%.20f\n", F2);//%lf%f

Long double f3 = 34567.345678987654345678987654;

printf ("%lf\n", F3);//%lf

return 0;

}

ios-c_day2___ binary Code

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.