From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BANG_GUAR,BAYES_00 autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c9d5fc258548b22a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news2.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe10.iad.POSTED!1d9d5bd3!not-for-mail From: David Thompson Newsgroups: comp.lang.ada Subject: Re: How do I write directly to a memory address? Message-ID: <6erul698mp5f3340us9o8h60j0eemp3jp0@4ax.com> References: <1f229967-d3cf-42b6-8087-c97ee08652f3@i40g2000yqh.googlegroups.com> <4d51169e$0$7657$9b4e6d93@newsspool1.arcor-online.net> <1bnp0pw1c8r5b$.guxc48qweiwe.dlg@40tude.net> <1ju2bba947c1h.y05qev0wjx2t.dlg@40tude.net> <44254ef2-5066-414d-8c1f-1021f9a9ebc9@o18g2000prh.googlegroups.com> X-Newsreader: Forte Agent 3.3/32.846 MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@teranews.com NNTP-Posting-Date: Mon, 21 Feb 2011 07:17:18 UTC Organization: TeraNews.com Date: Mon, 21 Feb 2011 02:17:37 -0500 Xref: g2news2.google.com comp.lang.ada:18479 Date: 2011-02-21T02:17:37-05:00 List-Id: On Tue, 8 Feb 2011 13:31:29 -0800 (PST), Shark8 wrote: > On Feb 8, 1:44�pm, "Dmitry A. Kazakov" > wrote: > > > > The next test was this: > > > > � �x and FF = ? > > � �x or FF = ? > > � �x xor FF = ? > > > > ...w.t.f. > That's insane... how is it I'm not even getting to the interview > stage? > I could do that; though I'm loathe to in C/C++. > > // we do have a bool type on this compiler, no? In C++ always; in C99 the functionality is standard under the name _Bool, but to have the name bool you must #include But bool/_Bool is a single true/false value, see below. > bool and ( int v1, int v2 ) See below. > { int result = 0; > for (int index = 7; index>=0; --index) > { result |= (( v1>>index && v2>>index ) && 1) << index; } This doesn't do what any sensible person wants; it sets bit # index in the result if bit # index *or any bit larger in magnitude* (numbered higher in your numbering) is set in each of v1 and v2. If you don't want to just do builtin bitwise v1 & v2, you can do ... ( (v1>>index)&1 && (v2>>index)&1 ) << index ... or ... ( v1 & (1<>= 1 ) result |= (v1 & mask) && (v2 & mask) ? mask: 0 which you can further modify to for( mask = 1<<7; mask!=0; mask >>= 1 ) result |= v1 & v2 & mask; at which point it's pretty obvious to just do result = v1 & v2; > return result; If you do compute the bitwise-and correctly above, then for a bool return type, this would return true=1 if *any* bit is set, false=0 if none. That makes it useless for the way you try to use it below. You might as well use int for the return since your inputs did, although it would be better to make them both unsigned char, the only C type likely (but not guaranteed!) to be exactly 8 magnitude bits. > int not_and = !and( v1, v2 ); ! is 'boolean' complement; it is 1 if the operand is all-zero-bits, and 0 if the operand has any-one-bits. You want ~ bitwise. > int _or = or( v1, v2 ); Most identifiers beginning with underscore are reserved to the implementation e.g. for 'private' bits in standard headers, since C can't make them invisible. Some subsets are available, and this one is among them, but a competent reviewer/etc. will need to check. It's easier to eschew such identifiers and safely avoid any problem. C++ also reserves double-underscore *anywhere* in identifiers.