|
| chrisname (2552) | |||
| I thought I had understood this :l 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.
With this code I get 0xa and 0xaf0. How can I get the first 4 bits in hex1 and the last 4 bits in hex2? I'm pretty new to shifting... | |||
Last edited on | |||
| Bazzy (4118) | |||
You need the bitwise and:
| |||
| chrisname (2552) | |||
| I thought it would have something to do with bitwise and, or or xor. I was playing around with them. Why is it 0xF? Because F is the largest Hex value? It worked; thanks :) I like this now:
I never understood bitwise shifting in the slightest before :P | |||
Last edited on | |||
| Bazzy (4118) | |||
| |||
| chrisname (2552) | |
| Oh; no wonder they use hex for low level stuff! 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? | |
| Bazzy (4118) | |
0x... is an integer so it will contain 2*sizeof(int) digits | |
| chrisname (2552) | |
| Oh. Well, I'm gonna spend some time playing with this stuff. Thank you for the help :) | |
| Bazzy (4118) | ||
self quoting:
Just realised this works only if CHAR_BIT == 8, the right formula is sizeof(int)*16/CHAR_BITAnyway, who uses non 8-bit bytes any more? | ||
| mcleano (733) | ||
Me neither! Still don't to be honest. Where is this useful? All I ever hear is when you are dealing with file compression and have to work with bits. | ||
| Bazzy (4118) | |
| Knowing how to get a specific byte from a longer value is useful in many situations | |
| chrisname (2552) | |
| What is CHAR_BIT? sizeof(char)? Or the particular bit I want to get? | |
| Bazzy (4118) | |
Is the size of a char in bits ( sizeof returns the size in bytes )http://www.cplusplus.com/reference/clibrary/climits/ | |
Last edited on | |
| chrisname (2552) | ||||
I'm trying this; but I'm having trouble figuring out how much to shift the number by...
| ||||
Last edited on | ||||
| helios (6075) | ||||
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:
| ||||
| chrisname (2552) | ||||
Typo :)
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.
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. | ||||
Last edited on | ||||
| helios (6075) | |
| 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). | |
Last edited on | |
| chrisname (2552) | ||
| 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
| ||
Last edited on | ||
| helios (6075) | |
| Wow. Now that's what I call miscounting. | |
| chrisname (2552) | |
| What, you, or me? Hopefully you; unless my whole perception is broken. Edit: I thought I'd need bitwise shifts to represent 8-bit colours (don't ask why), e.g. 0x0F is a black background with white text. | |
Last edited on | |
This topic is archived - New replies not allowed.
