# How to write numbers with hexadecimal programming



## GSquadron (Dec 4, 2009)

How to write numbers with hexadecimal programming?


----------



## dcf-joe (Dec 4, 2009)

What exactly do you mean?


----------



## W1zzard (Dec 4, 2009)

you mean how to write them in source code? depends on the programming language.

common notations are

0xfb
$fb
fbh


----------



## Braveheart (Dec 16, 2009)

it depends on the language...and why would you need to write Hex in another language? Hex is just a way of representing numbers.


----------



## GSquadron (Dec 16, 2009)

I need them for school! Anyway it does not depend if u dont have a program, just a black board :SSS. The professors are going to add them as a question even if they didnt explain these numbers!


----------



## W1zzard (Dec 16, 2009)

as long as you use a consistent notation you will be fine. i recommend suffix h for hex and b for binary

unless your professor has told you to use another notation.


----------



## FordGT90Concept (Dec 16, 2009)

Hexadeximal does represent numbers...

0=0
1=1
2=2
3=3
4=4
5=5
6=6
7=7
8=8
9=9
A=10
B=11
C=12
D=13
E=14
F=15

In decimal, every digit is worth 10.  In hexadecimal, every digit is worth 16.

0xF = 15
0xFF = 255
0xFFF = 4095
0xFFFF= 65535


----------



## GSquadron (Dec 16, 2009)

What about 3A8? Can anyone explain me these cuz i have forgotten them a bit and the worse think is that they are not in the book! Anyway i really need help with negative numbers


----------



## Munki (Dec 16, 2009)

Aleksander Dishnica said:


> What about 3A8? Can anyone explain me these cuz i have forgotten them a bit and the worse think is that they are not in the book! Anyway i really need help with negative numbers



936


----------



## dcf-joe (Dec 16, 2009)

3A8 can be represented in binary as 1110101000. You just have to break each individual number or character to its binary equivalent. Then, you can convert that binary to decimal.


----------



## GSquadron (Dec 16, 2009)

Anyone here couldn't tell me how dcf found the number was 1110101000? I need the explanation not the number cuz i took it as an example
Anyway thx :S


----------



## Munki (Dec 16, 2009)

Aleksander Dishnica said:


> Anyone here couldn't tell me how dcf found the number was 1110101000? I need the explanation not the number cuz i took it as an example
> Anyway thx :S




you have 8 bits in an octet xxxxxxxx - each with a base 2 system so going from the right x to left you have 1's | 2's | 4's | 8's | 16's | 32's | 64's | 128's | ect.


----------



## shevanel (Dec 16, 2009)

"tpu.. helping kid's with their homework since 1998"


----------



## Munki (Dec 16, 2009)

shevanel said:


> "tpu.. helping kid's with their homework since 1998"


----------



## Munki (Dec 16, 2009)

Hex - 3a8
Bi - 1110101000
Dec - 936


-----The breakdown------

936 - 512 = 424
424 - 256 = 168
168 - 128 = 40
40 - 64 = N/A
40 - 32 =  8
8 - 16 = N/A
8 - 8 = 0 EQUILIBRIUM!
0 - 4 = N/A
0 - 2 = N/A
0 - 1 = N/A


----------



## Kreij (Dec 16, 2009)

shevanel said:


> "tpu.. helping kid's everyone with their homework since 1998"



Fixed. lol

You guys never had to teach early freshmen have you?

To convert hex to binary ...

Take original number (3A8) and convert each digit to decimal then to binary
3 = 3 = 0011
A = 10 = 1010
8 = 8 = 1000

line them up ... 0011 1010 1000, remove leading zeros and cram together ... 1110101000

It's magical !! (snif)


----------



## dcf-joe (Dec 16, 2009)

There you go Aleksander, kreij just explained how I got that binary number. I am sorry for not explaining my work


----------



## v12dock (Dec 17, 2009)

http://easycalculation.com/hex-converter.php


----------



## Munki (Dec 17, 2009)

Damn...I just noticed he wanted to know how to get hex. I read it but not good enough obviously.


----------



## FordGT90Concept (Dec 17, 2009)

Aleksander Dishnica said:


> What about 3A8? Can anyone explain me these cuz i have forgotten them a bit and the worse think is that they are not in the book! Anyway i really need help with negative numbers


Hex value: digit value to the power of significant digits times decimal value of digit equals decimal value
Hex value: digit value ^ significant digits * decimal value of digit = decimal value

3: 16^2 * 3 =  768
A: 16^1 * 10 = 160
8: 16^0 * 8 = 8

Add the decimal value of each digit together (768 + 160 + 8) and you get 936.

0x3A8 = 936


If you want negative, you have to break it down into binary where one bit is a positive/negative flag.  That depends if you are using big or small endian.  Really, it depends on what ISO you are using.


----------



## Kreij (Dec 17, 2009)

In addition for Ford's expaination, if you need to convert hex to decimal it will become a lot easier if you memorize the first 4 or five numbers

16^0 = 1 (any number to the zero power equals 1)
16^1 = 16
16^2 = 256
16^3 = 4096
16^4 = 65356

You can then do
(3 * 256) + (10 * 16) + (8 * 1)


----------



## lemonadesoda (Dec 17, 2009)

Handy tips from an old timer machine code programmer.

Learn your binaries 0, 1
Learn your nibbles 0000, 0001, 0010, 0011, 0100, ... , 1110, 1111
Learn your hex 0, 1, 2, 3, 4, ...., E, F
Don't worry about your octets. Nibbles are easier, and 2 binary nibbles is an octet.

Your nibbles match your hex

Learn how to NOT, that is, in binary, swap 0 for 1 and 1 for 0.
NOT-ing nibbles is the same, 0000 for 1111, 0001 for 1110 etc.
NOT-ing hex is the same concept, just learn them, 0 for F, 1 for E, 2 for D etc.

When you have negative numbers there are different methods, but at machine level, you do this:

Decide what the biggest positive number is that you want to show. Typically in an 8bit format, when you do *signed-arithmetic*, that means there are negative numbers, then the leading bit must be 0. Hence you could have 01111111 = 127, or 7F as the maximum positive number in signed-8bit.  In 16bit format it would be 0111111111111111 or 7FFF.

Remember, the largest binary number in in 8bits is 255. But in signed-8bit it is 127.

A negative number is when the leading bit is 1.  And -1 in 8bit is 11111111, or 1111111111111111 in 16bit.

To make a positive number negative, you NOT the bits and add one.

e.g. 1=00000001
NOT 1 = 11111110
NOT 1 add 1 = 11111111 = -1

Easy.

In Hex

1 = 01h
NOT 1 = FEh
NOT 1 add 1 = FFh = -1

OK, lets do 127

In binary
127 = 01111111
NOT 127 = 10000000
NOT 127 add 1 = 10000001 = -127

In hex
127 = 7Fh
NOT 127 = 80h
NOT 127 add one = 81h = -127

Oh look, 127 was the largest possible number in signed-8bits, but there is a smaller negative number than -127. It is -128 or 80h!

******

I dont know what that cowboy Ford is going on about with his ISOs and big and little endians! 

The ISO, well actually, the IEEE, sets standards for integer and floating point notation, formats, rounding and accuracy. 

Big and little endian is about how the computer stores data, with the "big part" first, or the "little part" first. This is a purely memory mapping issues and is somewhat irrelevant to the math.

So what Ford is saying is that your representation of a number, or its negative, is only true in "the format as YOU defined it". And you should really use an accepted method, following the ISO/IEEE standard.  Your college professor teacher should have told you what number format/standard to follow. IF NOT, then you need to state it up front!


----------



## Kreij (Dec 17, 2009)

> Learn your binaries 0, 1



In a class of people new to base number conversions, everything after this line will immediately produce a very pronounced "doe in the headlights" look. 

When I was working on my degree in electronics, I got a job as a faculty assistant.
My job was to teach them about highly complex mathimatical formulas like, V = I * R

At least 50% of the class had problems with Ohm's Law.

Not suprisingly, when they finally got to digital gate curcuitry (ie, not, and, or, nor, etc.) the class sizes had dwindled to aprox. 50% of the starting people.

At graduation, there were about 25% of the starting class and about 5% of those had a GPA of 3.25 or better.


----------



## lemonadesoda (Dec 17, 2009)

True.

Fortunately for us, the OP didnt have a US education. So we can assume he's intelligent, just uninformed. 

You know, there are some things you really cant teach effectively and expect (uninterested) students to understand. They need to teach themselves during the process of trying to solve a problem they want to solve.  I learned (self taught) my binary math when I was 11 or 12.  First on the Z80, then on the 6502.

Just be grateful I didnt go into explaining BCD, Binary coded decimal. Or explain how a processors carry flag works when you add or sbc or roll


----------



## Kreij (Dec 17, 2009)

Good luck picking a custom title, Lemon  (4999 posts)

In my experience it was not just that they were uninterested. I blame a lot of it on the math teachers they had in elementary and high school. I've hear from more than one person who had problems in math have their teacher tell them, "don't worry about it. Math is hard."


----------



## GSquadron (Dec 17, 2009)

Thank you guys, but as a mere luck my teacher today explained every thing. Even though at the end of the lesson i made him a question: We start from left - right, or right - left?
The teacher said: Oh i totally forgot and that makes a lot of trouble for my students. Anyway i am saying this thing only to you. It depends totaly on the standart u are using. If it is IEEE than your start from left - right. Than in binary we always start from right to left.
I was really impressed cuz this was all i couldn't understand since the beginning. Now i am well informed. Thank you guys and especially Kreij and lemonadesoda for explaining me the hexadecimal negative numbers cuz they were out of discussion during the lesson :S


----------



## Kreij (Dec 18, 2009)

shevanel said:


> "tpu.. helping kid's with their homework since 1998"



Shev, I'm going to modify that a little and steal it for my Sig. You get my 100th thanks in appreaciation. 



Aleksander Dishnica said:


> Thank you guys and especially Kreij and lemonadesoda for explaining me the hexadecimal negative numbers cuz they were out of discussion during the lesson :S



No problem, Aleks, You know where to find us if you need more assistance.


----------

