Hi all. I have this C++ program where an unsigned char holds some status bits. I need to reset one of them. In C++ the available bit-wise operators ar AND, OR and XOR, not NOT: unsigned char f = 0xA8; // f = 10101000 in binary unsigned char m = 0x02l // m = 00000010 in binary To set the 2nd rightmost bit in f, we set f = f & m; // f now holds 10101010 in binary Formally, it is easy to clear the same bit: f = f AND NOT(m) but how does one implement this when C++ does not come with a bit-wise NOT operator? Rune
C/C++: Bit-wise NOT
Started by ●May 16, 2007
Reply by ●May 16, 20072007-05-16
Rune Allnor wrote:> Hi all. > > I have this C++ program where an unsigned char holds some status > bits. > I need to reset one of them. In C++ the available bit-wise operators > ar > AND, OR and XOR, not NOT:Go read your textbooks, shamefull student ;-)> > unsigned char f = 0xA8; // f = 10101000 in binary > unsigned char m = 0x02l // m = 00000010 in binary> To set the 2nd rightmost bit in f, we set > > f = f & m; // f now holds 10101010 in binary???? f |= m;> Formally, it is easy to clear the same bit: > > f = f AND NOT(m) > > but how does one implement this when C++ does not > come with a bit-wise NOT operator?f &= ~m; Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Reply by ●May 16, 20072007-05-16
Rune Allnor wrote:> Hi all. > > I have this C++ program where an unsigned char holds some status > bits. > I need to reset one of them. In C++ the available bit-wise operators > ar > AND, OR and XOR, not NOT: > > unsigned char f = 0xA8; // f = 10101000 in binary > unsigned char m = 0x02l // m = 00000010 in binary > > To set the 2nd rightmost bit in f, we set > > f = f & m; // f now holds 10101010 in binary > > Formally, it is easy to clear the same bit: > > f = f AND NOT(m) > > but how does one implement this when C++ does not > come with a bit-wise NOT operator?Negate and decrement. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Reply by ●May 16, 20072007-05-16
On Thu, 17 May 2007 00:23:56 GMT, the renowned Vladimir Vassilevsky <antispam_bogus@hotmail.com> wrote:> > >Rune Allnor wrote: > >> Hi all. >> >> I have this C++ program where an unsigned char holds some status >> bits. >> I need to reset one of them. In C++ the available bit-wise operators >> ar >> AND, OR and XOR, not NOT: > >Go read your textbooks, shamefull student ;-) > >> >> unsigned char f = 0xA8; // f = 10101000 in binary >> unsigned char m = 0x02l // m = 00000010 in binary > > > >> To set the 2nd rightmost bit in f, we set >> >> f = f & m; // f now holds 10101010 in binary > >???? > >f |= m; > > >> Formally, it is easy to clear the same bit: >> >> f = f AND NOT(m) >> >> but how does one implement this when C++ does not >> come with a bit-wise NOT operator? > >f &= ~m;Or you can use 'compl' Best regards, Spehro Pefhany -- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
Reply by ●May 17, 20072007-05-17
Rune Allnor wrote:> Hi all. > > I have this C++ program where an unsigned char holds some status > bits. > I need to reset one of them. In C++ the available bit-wise operators > ar > AND, OR and XOR, not NOT: > > unsigned char f = 0xA8; // f = 10101000 in binary > unsigned char m = 0x02l // m = 00000010 in binary > > To set the 2nd rightmost bit in f, we set > > f = f & m; // f now holds 10101010 in binary > > Formally, it is easy to clear the same bit: > > f = f AND NOT(m) > > but how does one implement this when C++ does not > come with a bit-wise NOT operator? > > Rune >~a = bitwise not of a. In C and C++. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" gives you just what it says. See details at http://www.wescottdesign.com/actfes/actfes.html
Reply by ●May 17, 20072007-05-17
Tim Wescott wrote: (snip)>> but how does one implement this when C++ does not >> come with a bit-wise NOT operator?> ~a = bitwise not of a. In C and C++.And Java. PL/I has the � operator, which is a normal EBCDIC character, but not ASCII. Some conversion table translate the ASCII ~ to the EBCDIC NOT (�) character. Fortran has the NOT function. -- glen
Reply by ●May 17, 20072007-05-17
Vladimir Vassilevsky wrote:>> but how does one implement this when C++ does not >> come with a bit-wise NOT operator? > > f &= ~m;I once worked with a guy who did a lot of low-level embedded coding. One time I found this in his code: if (~ strcmp (a, b)) /* Whatever */ ; Fortunately that guy now works as a patent attorney. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- J. Headley: "God, root, what is difference ?" G. Haverland: "God can change the byte order on the CPU, root can't."
Reply by ●May 17, 20072007-05-17
Erik de Castro Lopo wrote:> I once worked with a guy who did a lot of low-level embedded > coding. One time I found this in his code: > > > if (~ strcmp (a, b)) > /* Whatever */ ; > > Fortunately that guy now works as a patent attorney.This reminds me the old anecdote: After many years, a guy meets his girlfriend from high school. She asks him: - What are you doing? - I am the engineer! And what are you doing? - I am the prostitute. - Fie! You suck dicks... - No. It is you who suck dicks, and I am the prostitute. VLV
Reply by ●May 17, 20072007-05-17
Vladimir Vassilevsky wrote:> > > Erik de Castro Lopo wrote: > > >> I once worked with a guy who did a lot of low-level embedded >> coding. One time I found this in his code: >> >> >> if (~ strcmp (a, b)) >> /* Whatever */ ; >> >> Fortunately that guy now works as a patent attorney. > > This reminds me the old anecdote: > > After many years, a guy meets his girlfriend from high school. She asks > him: > - What are you doing? > - I am the engineer! And what are you doing? > - I am the prostitute. > - Fie! You suck dicks... > - No. It is you who suck dicks, and I am the prostitute.The traditional related comment from UK engineers is "Don't tell my mum I'm an engineer. She thinks I play piano in a brothel." Steve
Reply by ●May 17, 20072007-05-17
On 17 May, 02:23, Vladimir Vassilevsky <antispam_bo...@hotmail.com> wrote:> Rune Allnor wrote: > > Hi all. > > > I have this C++ program where an unsigned char holds some status > > bits. > > I need to reset one of them. In C++ the available bit-wise operators > > ar > > AND, OR and XOR, not NOT: > > Go read your textbooks, shamefull student ;-)Wish I could, but I will be at sea for another couple of weeks. The online help in my C++ program only mentions OR, AND and XOR under "bitwise operators".> > unsigned char f = 0xA8; // f = 10101000 in binary > > unsigned char m = 0x02l // m = 00000010 in binary > > To set the 2nd rightmost bit in f, we set > > > f = f & m; // f now holds 10101010 in binary > > ????Typo. Not '&', '|' Rune






