Need moar posts like this ^^
Here's what going on in your first program, I'll annotate your code:
#include <stdio.h>
int main (void)
{
int a[5] = {5,4,3,2,1}; //a is not an int array that holds five int values
//for instance lets say the address of a[0] is 0x50
int i = 2; //an int called i is assigned a value of 2
int *p = &a[2]; //now you are declaring a pointer to the THIRD element
//(remember 0 based arrays in c) the third element has an address of 0x58
//notice the EIGHT on the end, why is this? because the size of an int
//is such that the elements in the int array have the address 0x50, 0x54,
//0x58, 0x5c, 0x60
printf("%d %d", *(a+i), p[3]); // now you're printing out *(a+1) which is a[2] which is 3
// then you try to print out p[3] which in reality is like saying a[5], because you already assigned p to //point to the THIRD element in the array called 'a', if on the other hand you would have said p[2] you //would have gotten 1 but you said p[3] which is going to give you a random memory address
system("pause");
}
Those numbers in the array make it very hard for a student to understand because arrays are zero based, bad example on the teachers part.
I once lost an entire fucking day debugging about 128 individually masked bits coming in off a hardware board in Ada. Turned out the masking actually worked correctly, but the ass-backwards way the constants were coded and the complete lack of documentation forced me to redo it completely just so anyone who hadn't seen it before could have a PRAYER of figuring it out in less than 6 hours.
Also, thank you guys for giving me something to focus on other than my tech writing assignment... god I hate this class ~.~
It's not that you can't do the bitwise operations, its that it's near impossible (or it was, last time I tried) to break down a java object into bit-sized chunks so that you Can mask 8 or however many bits for an interface. Of course, that was like 2-3 years ago, so a new jdk probably added in support for that. But in the early days of java it was a nightmare, like doing a freaking hello world program. First hello world program I ever wrote that took like 500 lines.
If I'm thinking of it right, C# way of doing that would be somewhat like this...
ints wouldn't be big enough, ugly and can just use proper numerical value, but you get the point. That is probably a bassackwards way of going about it as well.... I need to look into it more!Code:int mask1 = 0x11111111; //Rightmost 8 bit mask int mask2 = 0x1111111100000000; //Next set of 8 int mask3 = 0x111111110000000000000000; //Next set of 8
Probably something with the << operator as well. ( bigMask & (smallMask << (8*maskNum))) maybe?
I prefer not to ever mess with hardware like this, so I've never personally studied it. I much prefer to just make high-end applications. So much easier and can have fun with it.
That's pretty much it, though obviously it's easier to maintain if you do like
int mask1 = 0b000000000000000011111111; //Rightmost 8 bit mask
int mask2 = 0b000000001111111100000000; //Next set of 8
int mask3 = 0b111111110000000000000000; //Next set of 8
etc etc...
Of course when you're dealing with lots of them then you don't make really big numbers, you just read them all into a data structure containing multiple ints or bytes or whatever which you mask individually. Easy to do in C with structs; not sure the best way to do it in C++ and later. I only really did that sort of thing in Ada & C so I can't really speak to it in newer languages.
Late, but pointer arithmetic does actually take into account the size of the type being pointed to. That's why we have typed pointers - if you have an int* on an architecture with 32-bit ints, the compiler knows that incrementing that pointer should actually increase it by 4 bytes, not 1. If you actually want to increment a pointer by physical bytes rather than logical units, you have to cast the pointer to a single-byte pointer type.
As an aside, beware opening gdb to debug your program and trying to print the value of *(&a+2) ... gdb will take the memory address of a, add two bytes, and print whatever is there, which may not be what you want.
Java land wants you to use EnumSets instead, which internally are bit fields.
And this right here is why my CS degree is a placemat and I chose to pursue an engineering role in field sales for a tech company.
Problem solving is fun. Debugging pointers is not.
I'm actually a little surprised I remember how everything works (been about 3 years since I've touched C), but I'd kill myself if I had to code day in and day out.
You can be a programmer without dealing with pointersPlenty of high-level languages out there