Inverting a 4 bit Value using BITWISE NOT- Troubles
DavidM
Posts: 630
Hi,
I have a value that is stored as a long variable in the range 1 to 10
I then need to INVERT That value as bits, but only the first 4 bits
eg, 1 = 0001 which needs to be inverted to "1110"
3 = 0011 which needs to be inverted to "1100"
9 = 1001 which needs to be inverted to "0110"
If I use the BITWISE NOT function ( i.e !) I think that all the "other" Bits 4-15 are being inverted to 1's
eg, 1 = 00000000_00000000_00000000_00000001
becomes 11111111_11111111_11111111_11111110 after I invert it
but I want it to be 00000000_00000000_00000000_00001110 , I only want the first 4 bits to be inverted
Any ideas?
Thanks
Dave M
I have a value that is stored as a long variable in the range 1 to 10
I then need to INVERT That value as bits, but only the first 4 bits
eg, 1 = 0001 which needs to be inverted to "1110"
3 = 0011 which needs to be inverted to "1100"
9 = 1001 which needs to be inverted to "0110"
If I use the BITWISE NOT function ( i.e !) I think that all the "other" Bits 4-15 are being inverted to 1's
eg, 1 = 00000000_00000000_00000000_00000001
becomes 11111111_11111111_11111111_11111110 after I invert it
but I want it to be 00000000_00000000_00000000_00001110 , I only want the first 4 bits to be inverted
Any ideas?
Thanks
Dave M
Comments
XOR it with 15.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
That worked!!
Thanks
Dave M