iOS Need some help with binary

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
Hey all,

I'm almost finished reading a book on Objective-C.

Book: Big Nerd ranch - Objective-C programming
Chapter: 33

It's going into bitwise operations and as such it starts out by giving us Base 10 examples then goes into binary (base 2) and finally Hex (base 16)

So I am having a hard time getting my head around this chapter. It seems my lack of math skills is haunting me. However, I want to understand this perfectly otherwise I am failing myself. I am generally good with numbers once I understand the basics.

So, let's start off at the beginning: Base 10 numbers.

I seem to understand this 100% - for example a number like: 123456 can be represented in decimal like so:

1 * 10^5 + 2 * 10^4 + 3 * 10^3 + 4 * 10^2 + 5 * 10^1 + 6 * 10^0

I think this gets me my 123456 number?

Now we go down to base 2 numbers:

What I am trying to figure out is this:

in binary I can work out these two numbers:

A. 10100010 = 162
B. 11001110 = 206

Now how I got this was using the diagram presented to us in this chapter - which looked something like this:

|128 |64 |32 |16| 8 |4 | 2 | 1
| 0 | 0 | 1| |1 |1 |1 | 0 | 0

What I am trying to understand is why is there (from right to left) 1 to 128 in the top?
The pattern shows that we multiply the number to the right by the power of 2 (base 2 numbers) - but if I try follow that rule it goes wrong quickly...

Eg:

1^ 2 = 1
2 ^2 = 4 ..so far so good..
4 ^ 2 = 16....

So where does 8 come into it?

If a look at it from right to lift - doubling up each time.

1 x 2 = 2.
2 x 4 = 4;
4 x 4 = 16

Where is 8?

I may have this completely wrong - but I need to understand this..

This is before I get to representing numbers in hex (Base 16)

So I need to get the binary part first.

Can anyone shed some light on this more me?

Thanks all
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Hey all,

I'm almost finished reading a book on Objective-C.

Book: Big Nerd ranch - Objective-C programming
Chapter: 33

It's going into bitwise operations and as such it starts out by giving us Base 10 examples then goes into binary (base 2) and finally Hex (base 16)

So I am having a hard time getting my head around this chapter. It seems my lack of math skills is haunting me. However, I want to understand this perfectly otherwise I am failing myself. I am generally good with numbers once I understand the basics.

So, let's start off at the beginning: Base 10 numbers.

I seem to understand this 100% - for example a number like: 123456 can be represented in decimal like so:

1 * 10^5 + 2 * 10^4 + 3 * 10^3 + 4 * 10^2 + 5 * 10^1 + 6 * 10^0

I think this gets me my 123456 number?

Now we go down to base 2 numbers:

What I am trying to figure out is this:

in binary I can work out these two numbers:

A. 10100010 = 162
B. 11001110 = 206

Now how I got this was using the diagram presented to us in this chapter - which looked something like this:

|128 |64 |32 |16| 8 |4 | 2 | 1
| 0 | 0 | 1| |1 |1 |1 | 0 | 0

What I am trying to understand is why is there (from right to left) 1 to 128 in the top?
The pattern shows that we multiply the number to the right by the power of 2 (base 2 numbers) - but if I try follow that rule it goes wrong quickly...

Eg:

1^ 2 = 1
2 ^2 = 4 ..so far so good..
4 ^ 2 = 16....

So where does 8 come into it?

If a look at it from right to lift - doubling up each time.

1 x 2 = 2.
2 x 4 = 4;
4 x 4 = 16

Where is 8?

I may have this completely wrong - but I need to understand this..

This is before I get to representing numbers in hex (Base 16)

So I need to get the binary part first.

Can anyone shed some light on this more me?

Thanks all
You have made a mistake.

In base 10, each digit to the left is a higher power of 10, right?

In binary, each digit to the left is a higher power of 2.

Code:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
What you wrote is

Code:
1^2 = 1
2^2 = 4 ..so far so good.. 
4^2 = 16....
You got confused and raised the base, not the power.

In all number systems, each higher digit raises the power. The base stays the same, and is the BASE of the number system. BASE 10, base 2, base 16. That's what that term means, AND how you remember it.
 
Last edited:

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
1 x 2 = 2.
2 x 4 = 4;
4 x 4 = 16

Where is 8?

I may have this completely wrong - but I need to understand this..
Use the same principle as before, but use two as base then raise to the power of 0, 1, 2, 3 and 4 and you will get: 1, 2, 4, 8, 16, the power here is the position in the binary number (0 being the first), so:

Code:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

or:

0001
0010
0100
1000
 

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
You have made a mistake.

In base 10, each digit to the left is a higher power of 10, right?

In binary, each digit to the left is a higher power of 2.

Code:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^3 = 16
What you wrote is

Code:
1^2 = 1
2^2 = 4 ..so far so good.. 
4^2 = 16....
You got confused and raised the base, not the power.

In all number systems, each higher digit raises the power. The base stays the same, and is the BASE of the number system. BASE 10, base 2, base 16. That's what that term means, AND how you remember it.
Okay wrong way round. I was raising the base - which is what you said - not the power.. makes sense.

I think however in your last example you meant 2^4 = 16 and not 2^3 - right?

Use the same principle as before, but use two as base then raise to the power of 0, 1, 2, 3 and 4 and you will get: 1, 2, 4, 8, 16, the power here is the position in the binary number (0 being the first), so:

Code:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8

or:

0001
0010
0100
1000
Thank you - now I understand it better. :)

Why start with 0 though and not 1?

Is this because the very first address in computer memory starts with 0?
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Why start with 0 though and not 1?

Is this because the very first address in computer memory starts with 0?
It's a consequence of how powers work, anything raised to the power of 0 is 1. There are proofs for it which I forgot, but I always thought of it like this:

Code:
2^-2 = 0.25
2^-1 = 0.5
2^0  = ?
2^1  = 2
2^2  = 4
As you go down in that list the number gets twice as large, and when you go up it get's twice as small, so 2^0 is at the junction of negative and positive powers, it has to be 1.
 

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
It's a consequence of how powers work, anything raised to the power of 0 is 1. There are proofs for it which I forgot, but I always thought of it like this:

Code:
2^-2 = 0.25
2^-1 = 0.5
2^0  = ?
2^1  = 2
2^2  = 4
As you go down in that list the number gets twice as large, and when you go up it get's twice as small, so 2^0 is at the junction of negative and positive powers, it has to be 1.
Not sure how we get 2^0 is 1. :confused:

But thanks for showing me how it works. I have a much better understanding of this now. Now it's time for hex. :(

Can I just add my Hex based questions here?
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Thanks Dejo. Will read the link tomorrow.

I used Base 16 and actually managed to work out how Hex is worked out.

I understand it 95% now.

Do we use hex and actual binary a lot in iOS programming or is it more for clarity sake on what's going on behind the scenes?
Hex is useful for looking at memory addresses.

Hex is also easy to convert to binary because each digit of a hex number expresses exactly 4 bits of binary

Code:
bin   hex
0000 $00
0001 $01
0010 $02
0011 $03
0100 $04
0101 $05
0110 $06
0111 $07
1000 $08
1001 $09
1010 $0A
1011 $0B
1100 $0C
1101 $0D
1110 $0E
1111 $0F
Every hex digit is a "nybble", so 2 hex digit expresses exactly 1 byte.

Decimal does not match well with binary.

It pays to be able to interpret both.

Old school UNIX C programmers use octal, which I never liked. In octal every octal digit expresses exactly 3 bits, not 4.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,575
4,005
Do we use hex and actual binary a lot in iOS programming or is it more for clarity sake on what's going on behind the scenes?
I doubt you'll touch hex or binary yourself much. But everything in the computer is stored in binary, so yes, that's what's going on behind the scenes, and I'd say it's great to understand that. Instructions are strings of bits. Data is strings of bits. Between instruction and data, that's basically everything your computer handles.
 

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
Thanks guys.
Much appreciated.

I get how hex works however I can convert a number into hex but converting a hex value into a number / binary is where I am finding that part difficult.
 

steveuk36

macrumors newbie
Oct 12, 2011
5
0
Converting hex to binary

Hex F34

split this into 3 different entities

F 3 4

then just use a similar thing to how you worked out the binary from earlier

F
8|4|2|1
1 1 1 1

3
8|4|2|1
0 0 1 1

4
8|4|2|1
0 1 0 0

so F = 15 in hex 8+4+2+1 = 15 therefore all the binary numbers in the first part should be 1's


3 just needs the 2 and 1 to be converted to 1's and so on for the 4

111100110100 is binary for the hex number F34, then you could just use the same calculation to convert binary to decimal that you have already understood

I hope i have explained that so you understand it
 
Last edited:

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
Converting hex to binary

Hex F34

split this into 3 different entities

F 3 4

then just use a similar thing to how you worked out the binary from earlier

F
8|4|2|1
1 1 1 1

3
8|4|2|1
0 0 1 1

4
8|4|2|1
0 1 0 0

so F = 15 in hex 8+4+2+1 = 15 therefore all the binary numbers in the first part should be 1's


3 just needs the 2 and 1 to be converted to 1's and so on for the 4

111100110100 is binary for the hex number F34, then you could just use the same calculation to convert binary to decimal that you have already understood

I hope i have explained that so you understand it
Thank's Steve.

I can see how you get that - let me give another example to see if I am getting this?

Hex: F52:

F = 15. So:

|8|4|2|1
|1|1|1|1

5
|8|4|2|1
|0|1|0|1

2
|8|4|2|1
|0|0|1|0

F52 into binary: 111101010010 - right?
 

steveuk36

macrumors newbie
Oct 12, 2011
5
0
Thank's Steve.

I can see how you get that - let me give another example to see if I am getting this?

Hex: F52:

F = 15. So:

|8|4|2|1
|1|1|1|1

5
|8|4|2|1
|0|1|0|1

2
|8|4|2|1
|0|0|1|0

F52 into binary: 111101010010 - right?
Spot on
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
FYI, The Mac Calculator app can do octal, hex, and binary. Just change View > Programmer.

I mainly use it for converting between hex and decimal and doing hex math.

Did you learn about endianness yet? (big endian, little endian).
 

Tander

macrumors 6502a
Original poster
Oct 21, 2011
676
1
Johannesburg, South Africa
FYI, The Mac Calculator app can do octal, hex, and binary. Just change View > Programmer.

I mainly use it for converting between hex and decimal and doing hex math.

Did you learn about endianness yet? (big endian, little endian).
Thanks for the tip - didn't know that! :cool:

To answer your question - since I don't know what you're talking about, I'm going to say no. :(
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,575
4,005
Thanks for the tip - didn't know that! :cool:

To answer your question - since I don't know what you're talking about, I'm going to say no. :(
Endianess = is the most significant digit first or last?

I can't ever remember which is which, but in one endianess, 8 would be written:

1000

while in the other it would be written

0001

6 would be written

1010

In the first form or

0101

In the other.

In my experience, most microchips use the first form, not the second.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Endianess = is the most significant digit first or last?
It refers to byte order, so it makes no difference on a single byte.

Most significant byte first is big endian and is used by PowerPC, ARM, the internet and humans. Least significant byte first is used by intel.

The effects can be seen by doing like this for example:

Code:
int main()
{
    unsigned int a = 0xdeadbeef;
    write(STDOUT_FILENO, &a, sizeof(a));

    return 0;
}
Then:

Code:
./a.out | xxd
0000000: efbe adde                                ....
 

firewood

macrumors 604
Jul 29, 2003
7,641
873
Silicon Valley
Do we use hex and actual binary a lot in iOS programming or is it more for clarity sake on what's going on behind the scenes?
The iOS API includes a lot of constants that consist of composite bit masks. So knowing binary will help when you have to figure out what bits were turned on in some number that the debugger shows you after your app crashes.

The instruction set that the CPUs that Apple uses (x86, arm) address memory in terms of bytes (not all computer ISA's do). And the bits represented by 2 hex digits fits inside each byte, which might make the print out of an unformatted memory core dump during debugging more readable when using hex.

Debugging your mistakes (and you, like every other programmer, will make many many) is a lot easier if you know what's going on behind the scenes.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Sorry about the endianness tangent. Read about it here here. On the one hand it's become a little-endian world with the popular cpu architectures being all little-endian these days. On the other hand it hardly matters. When programming in a high level language like Objective-C I don't often notice that the bytes are backwards.

You can't call yourself a programmer if you don't understand bits and bytes, hex and binary. There are plenty of bitmasks in the Apple headers. You may not need to write them but you need to understand them. There are also bitfields but they're private.
 

xArtx

macrumors 6502a
Mar 30, 2012
764
1
It was funny getting used to counting from zero.
We don't do it because it takes a count of one to make something significant
like one apple. If the number of lives you have in a game = zero, then
you will have to know about it, but in real life who's counting the number of lives they have?

I never figured out the inconsistency where arrays are explicitly declared:
Code:
int elements[8] = {1,2,3,4,5,6,7,8};
actually means eight elements in Human talk, but when you address them,
you have to start from zero again, and elements[0] has a value of 1 in it.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Thank's Steve.

I can see how you get that - let me give another example to see if I am getting this?

Hex: F52:

F = 15. So:

|8|4|2|1
|1|1|1|1

5
|8|4|2|1
|0|1|0|1

2
|8|4|2|1
|0|0|1|0

F52 into binary: 111101010010 - right?

You're getting it.

When I was learning programming I wrote a base converter.

It's a great exercise that really reinforces the concepts strongly.

It's actually not that hard to write a base converter that will convert from any base to any base, from base 2 to base 36 (Base 36 because you can easily use the digits 0-9 and the entire alphabet from A to Z, but after that you run out of symbol and have to resort to special characters.)

The trick is to set up an array of digits from 0 to Z, and index into that array to fetch an output character for a digit.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
I never figured out the inconsistency where arrays are explicitly declared:
Code:
int elements[8] = {1,2,3,4,5,6,7,8};
actually means eight elements in Human talk, but when you address them,
you have to start from zero again, and elements[0] has a value of 1 in it.
It's because array indices are just syntactic sugar to dereference and add an offset to the start position. You could write it like this as well:

Code:
int main()
{
    int elements[8] = {1,2,3,4,5,6,7,8};

    printf("%d, %d\n", elements[0], elements[1]);
    printf("%d, %d\n", *elements, *(elements + 1));

    return 0;
}
Here I could have written: *(elements + 0) as well, but it's pointless since adding 0 to the base address will not change anything.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,575
4,005
You're getting it.

When I was learning programming I wrote a base converter.

It's a great exercise that really reinforces the concepts strongly.

It's actually not that hard to write a base converter that will convert from any base to any base, from base 2 to base 36 (Base 36 because you can easily use the digits 0-9 and the entire alphabet from A to Z, but after that you run out of symbol and have to resort to special characters.)

The trick is to set up an array of digits from 0 to Z, and index into that array to fetch an output character for a digit.
Why not 62? 0-9, a-z, A-Z