# OSDev.org

The Place to Start for Operating System Developers
 It is currently Mon Jan 22, 2018 8:32 am

 All times are UTC - 6 hours

 Page 2 of 2 [ 17 posts ] Go to page Previous  1, 2
 Print view Previous topic | Next topic
Author Message
 Post subject: Re: Printing Hex Values (Lang: C)Posted: Mon Oct 10, 2016 3:07 pm
 Member

Joined: Tue Aug 02, 2016 1:52 pm
Posts: 286
Location: East Riding of Yorkshire, UK
octacone wrote:
P.S. I know what an integer is, but you cannot make letters like A, B, C, D, E, F become an int.

Why not? You can write a number any way you want. 2016 = 0x7E0 = 0o3740 = MMXVI...
They all mean the same thing, just different ways of representing it.

_________________
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
Compiler Development Forum

Top

 Post subject: Re: Printing Hex Values (Lang: C)Posted: Wed Oct 12, 2016 5:52 pm
 Member

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1059
Location: Athens, GA, USA
octacone wrote:
P.S. I know what an integer is, but you cannot make letters like A, B, C, D, E, F become an int.

I am not so certain you do know what an int is, because you seem to think that they have something to do with the numerals '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', which is not the case. The decimal numerals are just a representation of the abstract concept of an integer, just as a value stored as a string containing hexadecimal digits ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F') is, a string containing binary digits ('0' and '1') is, as a set of differential currents in a circuit (which is what all data in a conventional CPU actually is) is, or a string holding the Roman numerals 'I', 'V', 'X' 'L', C', 'M' is (as zenzizenzicube already stated).

The value of an int data object representing the decimal value 6 is an sequence of currents which we usually represent as the binary number 0110; but the character '6' is represented on most computers today as the ASCII (or UTF-8) value whose binary representation is 011 0110, which would, if used as an integer instead of a character (because data in memory has no types in stock computers), would have the value 54 decimal, or 0x36 hex, or 066 octal. They represent that same value, but in different ways.

So what you are actually converting, when you convert an int to a string, is a binary value, and you can convert it to any string representation you choose, even the word "six" if you really want. The fact that you input the value as a hex value has no bearing on it once the compiler converts it to an int in the first place.

I suspect that this last point is the real problem that you are having: that you see the value represented in hex in the source code, and somehow think that it is still a hex value once it is compiled. That simply isn't the case: when the compiler's lexical analyzer matches a token that is a numeric literal, it stores the string representation for the value, and pass that around up to some point, but eventually it will convert that token into a binary integer - regardless of whether you wrote it as "0x0A" or "10" or "012", it still becomes the same sequence of currents (not exactly 'on' and 'off', since they are both greater than zero current, but close enough).

Again, the 'binary integer' is itself a representation of an abstract idea, but one which the CPU has specific operations which it can apply to it which manipulate it as if it were the mathematical construct it represents (up to a point). However, the CPU will just as readily apply to that value the instructions for loading an address, sending a value to the I/O bus, or any other opcode that will take a value that size or less. While 'tagged' architectures which check data type consistency do exist, none of the mainstream processors do (most modern ones use tagged memory in the cache memory systems, but not for typechecking), so it is the job of the compiler and the programmer to make sure that the types match, not the CPU.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 2 [ 17 posts ] Go to page Previous  1, 2

 All times are UTC - 6 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Operating System Development    OS Development    OS Design & Theory    Announcements, Test Requests, & Job openings Everything Else    General Programming    General Ramblings    Auto-Delete Forum OSDev.org    OSDev Wiki    About this site