search
HomeBackend DevelopmentC++Representing and Manipulating Information in modern computer - Part 2

Representing and Manipulating Information in modern computer - Part 2

Link to Part 1

Addressing and Byte ordering

A 4-byte int in a 32-bit machine stores all its 4 bytes in contigous sequence of bytes. It can be store in two way namely Little endian and Big endian depending on machine. Not going much into details a Little endian stores an int(4bytes on 32-bit) of hexadeciaml value 0x01234567 something like following(assuming starting address is 0x100):
Addresses/Values
0x100 67
0x101 45
0x102 23
0x103 01
and similary a Big endian will look like following:
Addresses/Values
0x100 01
0x101 23
0x102 45
0x103 67
I hope you can see the difference in the ordering. Linux 32bit, Windows, Linux 64bit follows Little endian whereas SunOS/SPARC follows Big endian.

This is important because when sending a message over a network from Little endian byte ordering machine to Big endian byte ordering machines and vice-versa could be an issue. Most of the programmers don't find it is an issue because networking applications are written in a way which does this convertions for us but if you are writting an network application, you might need to consider this.

Integer Arithmetic

You might be surpised to know, adding two positive number can result in a negative number and x

Let me give you an example, lets say we have a computer which stores an int as 4-bit and we have two unsigned int x and y.

unsigned int x = 10; // binary rep: 1010
unsigned int y = 15; // binary rep: 1111
unsigned int z = x y; // ???

The value of z is 25. right? right?

Well no. If you convert 25 into its binary representation, it comes out to be 11001 but as I mentioned our computer can only store 4-bit integers(values from 0-15 incase of unsigned). So, what will our computer do with the extra 1-bit? You are right, it will drop the higher-order bit(first bit from left) and we will get 1001 which converts to 9. This is same as doing module with 16 ie 25 mod 16=9. This behavior of computer not limited to arthmetic is also called Overflow.

But why I'm using unsigned int here? Will this addition behave differently with signed integers?

Answer: Yes but before explaining what will the result and how our computer ended up with that, lets first understand how signed and unsigned is different with our 4-bit size integer.

signed integers

They can store positive and negative both numbers values from -8(bin rep: 1000) to 7(bin rep: 0111). The higher-order bit(first bit from left) is the one which gives signed integer negative values and rest of the bits yields in positive. So, to get smallest number we need to flip higher-order to 1 and other bits 0 and to get largest number we need to flip higher-order bit to 0 and other bits to 1.

unsigned integers

They can only store positive numbers values from 0(binary rep: 0000) to 15(bin rep: 1111).

Now, because x=10 and y=15 will overflow before addition, we will use something smaller:
int x = 5; // 0101
int y = 6; // 0110
int z = x y // ???

The binary representation should be 1011 if we ignore signed consideration. As you can see, the higher-order bit is flipped to 1 and from above, the value of z will be -5(= -1*2ˆ3 2ˆ1 2ˆ0) instead of 11.

and also, adding two negative can result in postivie. eg,
int x = -8 // 1000
int y = -5 // 0101
int z = x y // ???

Now z will be -13 which is 10011 in binary(the higher-order bit is for negatives ie -1*2ˆ4 = -16) but our computer can only store 4-bits so it will drop higher-order bit and become 0011 which is 3 in decimal. Again, overflow.

This is why x

That's all for today. Please comment out if some information here is wrong or is missing. Thank you.

The above is the detailed content of Representing and Manipulating Information in modern computer - Part 2. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering Polymorphism in C  : A Deep DiveMastering Polymorphism in C : A Deep DiveMay 14, 2025 am 12:13 AM

Mastering polymorphisms in C can significantly improve code flexibility and maintainability. 1) Polymorphism allows different types of objects to be treated as objects of the same base type. 2) Implement runtime polymorphism through inheritance and virtual functions. 3) Polymorphism supports code extension without modifying existing classes. 4) Using CRTP to implement compile-time polymorphism can improve performance. 5) Smart pointers help resource management. 6) The base class should have a virtual destructor. 7) Performance optimization requires code analysis first.

C   Destructors vs Garbage Collectors : What are the differences?C Destructors vs Garbage Collectors : What are the differences?May 13, 2025 pm 03:25 PM

C destructorsprovideprecisecontroloverresourcemanagement,whilegarbagecollectorsautomatememorymanagementbutintroduceunpredictability.C destructors:1)Allowcustomcleanupactionswhenobjectsaredestroyed,2)Releaseresourcesimmediatelywhenobjectsgooutofscop

C   and XML: Integrating Data in Your ProjectsC and XML: Integrating Data in Your ProjectsMay 10, 2025 am 12:18 AM

Integrating XML in a C project can be achieved through the following steps: 1) parse and generate XML files using pugixml or TinyXML library, 2) select DOM or SAX methods for parsing, 3) handle nested nodes and multi-level properties, 4) optimize performance using debugging techniques and best practices.

Using XML in C  : A Guide to Libraries and ToolsUsing XML in C : A Guide to Libraries and ToolsMay 09, 2025 am 12:16 AM

XML is used in C because it provides a convenient way to structure data, especially in configuration files, data storage and network communications. 1) Select the appropriate library, such as TinyXML, pugixml, RapidXML, and decide according to project needs. 2) Understand two ways of XML parsing and generation: DOM is suitable for frequent access and modification, and SAX is suitable for large files or streaming data. 3) When optimizing performance, TinyXML is suitable for small files, pugixml performs well in memory and speed, and RapidXML is excellent in processing large files.

C# and C  : Exploring the Different ParadigmsC# and C : Exploring the Different ParadigmsMay 08, 2025 am 12:06 AM

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

C   XML Parsing: Techniques and Best PracticesC XML Parsing: Techniques and Best PracticesMay 07, 2025 am 12:06 AM

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C   in Specific Domains: Exploring Its StrongholdsC in Specific Domains: Exploring Its StrongholdsMay 06, 2025 am 12:08 AM

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

Debunking the Myths: Is C   Really a Dead Language?Debunking the Myths: Is C Really a Dead Language?May 05, 2025 am 12:11 AM

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),