I'm trying to shift a hex number to get the first and last bits in separate variables... For example, if I had the hex number 0xFA then I would get F in one variable and A in the other.
How would I do it for larger hex numbers?
This way I had to already know the number was 0xAF or 0xABCD; what if I can get anything from 0x0 to 0xFFFFFF? Obviously I'd use an array and some form of a loop; but then how would I know how big to make the array, and how many times to loop?
What the hell? G? This is hexadecimal, not heptadecimal.
sizeof(array) doesn't give the size of array in elements. It gives it in bytes. Your for on line 24 is overflowing the buffer.
The right formula would be sizeof(int)*CHAR_BIT/4:
With 16-bit bytes and 32-bit ints:
(32/4==8)
sizeof(int)*16/16 == 2*16/16 == 2
sizeof(int)*16/4 == 2*16/4 == 8
With 12-bit bytes and 36-bit ints:
(36/4==9)
sizeof(int)*16/12 == 3*16/12 == 4
sizeof(int)*12/4 == 3*12/4 == 9
Actually, the program is riddled with many small mistakes. Here:
What the hell? G? This is hexadecimal, not heptadecimal.
Typo :)
sizeof(array) doesn't give the size of array in elements. It gives it in bytes.
I usually use #define ARRAYSIZE(array) sizeof(array) / sizeof(array[0])
because then you get sizeof(array) (amount of bytes for the whole array) / sizeof(array[0]) (amount of bytes in the 0 element (which all arrays have, so it shouldn't segfault) which should mean sizeof(array) /(sizeof(data type of array) which should == amount of elements in the array.
At least, I hope so.
Actually, the program is riddled with many small mistakes. Here:
Thanks. I'll look through it.
Why are you using unsigned ints instead of just ints? Preference; or could the value get too large?
Edit: I just realised my password for everything is valid hex :l
That's only 10108 possible character combinations (amount of characters 10 numbers * 12 letters)!
CHANGE CHANGE CHANGE CHANGE.
It's not a good idea to use signed integers when doing bit twiddling. That's bitten me in the ass too many times. Just to name one example, char(0x80)>>7==(char)0xFF, when you'd expect it to be 1.
EDIT: 12 letters? Don't you mean 10 letters? ABCDEFabcdef. Also, your formula is wrong. n^(10+10).
Ok. I'm playing around in SDL at the moment; and I thought I would need this for some reason (I forget what I thought I was going to use it for). Oh well; it was as good a time as any to learn about shifting.
No:
A B C D E F
1 2 3 4 5 6
a b c d e f
7 8 9 10 11 12
F is the sixth letter in the basic modern Latin alphabet.