How does the bang operator (!) work against bits? - bit-manipulation

I've been asked to compute !x without using !.
Examples:
bang(3) = 0
bang(0) = 1
The legal operations: ~, $, |, ^, +, <<, >>
I don't understand what a bang does? I thought it was a factorial.

If in C, your "!" is a boolean inverse of its argument. So, !0 yields 1 and 1(anything else) yields 0. The "~" is the bitwise version of it, essentially flipping each bit in the (following) number - much easier to understand if you constrain yourself to unsigned numbers.
!x can be emulated with things like (x?1:0) - which isn't in your list - and the && and || operators - which also aren't in your list. "$" isn't an operator in C, so I'm a bit puzzled by that one - what language are you in? You might have some luck using the | and looping through all the bits to figure out if any are set, after that point using the bitwise operators on just your resulting 1 bit make it pretty easy to emulate "!".
I could just lay it out, but if you're taking a class, the struggle is where most of the gain is for this particular problem.

! is the logical negation operator. Your favorite search engine will tell you more.

Related

Express bitwise negation (`NOT`, bitwise complement) using other bitwise operations?

I am writing an algorithm in a limited language where the bitwise operators at my disposal are
AND: &
OR: |
XOR: ^
SRL: << (shift left)
SLL: >> (shift right)
I realized I need to be able to take the bitwise complement of an integer, commonly denoted ~x in other languages.
Could I somehow express ~x, using only the {&, |, ^, <<, >>} operators?
I would try to just implement this operator in the languageā€™s compiler, but it seems like a very challenging task. I would much rather do some dirty hack to express NOT x without ~.

Simplifying a bitwise operation

I have the following bitwise expression and was wondering if it could be simplified or if there is a general way of interpreting the output.
(x & y) | (~x & ~y)
The only "simpler" version is ~(x^y) (where ^ is the XOR operator).
However, It's not going to make a significant difference computationally, and is harder to read for most people (XOR is not as natural as AND and OR). There's not a simpler version using just AND and OR operators, so I would suggest you just leave it as-is.
if there is a general way of interpreting the output.
"Either both are true or both are false" is a natural interpretation.

Order of precedence

This is a really simple question, but I'm afraid it's a bit tricky.
Which of the following symbols has the highest precedence? (C++)
a. %
b. -
c. ||
d. &&
I know it sounds easy, but I'm really wondering if whether the answer is % or -. The symbol - can be seen as either a unary minus (which is higher than %) or subtraction (which is lower than %).
What do you think?
A google search for c++ Operator Precedence yielded this:
http://en.cppreference.com/w/cpp/language/operator_precedence
According to the link, Multiplication, division, and remainder come before Addition and subtraction and further down the list comes the Bitwise operations found in row 5,6,13, and 14 respectively.
I would assume the person who made this question wanted you to choose the operator that has the highest possible precedence.
I would choose b.

Processing conditional statements

I'm not looking for an implementation, just pseudo-code, or at least an algorithm to handle this effectively. I need to process statements like these:
(a) # if(a)
(a,b) # if(a || b)
(a+b) # if(a && b)
(a+b,c) # same as ((a+b),c) or if((a&&b) || c)
(a,b+c) # same as (a,(b|c)) or if(a || (b&&c))
So the + operator takes precedence over the , operator. (so my + is like mathematical multiplication with , being mathematical addition, but that is just confusing).
I think a recursive function would be best, so I can handle nested parentheses nice and easy by a recursive call. I'll also take care of error handling once the function returns, so no worries there. The problems I'm having:
I just don't know how to tackle the precedence thing. I could return true as soon as I see a , and the previous value was true. Otherwise, I'll rerun the same routine. A plus would effectively be a bool multiplication (ie true*true=true, true*false=false etc...).
Error detection: I've thought up several schemes to handle the input, but there are a lot of ugly bad things I want to detect and print an error to the user. None of the schemes I thought of handle errors in a unified (read: centralized) place in the code, which would be nice for maintainability and readability:
()
(,...
(+...
(a,,...
(a,+...
(a+,...
(a++...
Detecting these in my "routine" above should take care of bad input. Of course I'll check end-of-input each time I read a token.
Of course I'll have the problem of maybe having o read the full text file if there are unmatched parenthesis, but hey, people should avoid such tension.
EDIT: Ah, yes, I forgot the ! which should also be usable like the classic not operator:
(!a+b,c,!d)
Tiny update for those interested: I had an uninformed wild go at this, and wrote my own implementation from scratch. It may not be pretty enough for the die-hards, so hence this question on codereview.
The shunting-yard algorithm is easily implementable in a relatively short amount of code. It can be used to convert an infix expression like those in your examples into postfix expressions, and evaluation of a postfix expression is Easy-with-a-capital-E (you don't strictly need to complete the infix-to-postfix conversion; you can evaluate the postfix output of the shunting yard directly and just accumulate the result as you go along).
It handles operator precedence, parentheses, and both unary and binary operators (and with a little effort can be modified to handle infix ternary operators, like the conditional operator in many languages).
Write it in yacc (bison) it becomes trivial.
/* Yeacc Code */
%token IDENTIFIER
%token LITERAL
%%
Expression: OrExpression
OrExpression: AndExpression
| OrExpression ',' AndExpression
AndExpression: NotExpression
| AndExpression '+' NotExpression
NotExpression: PrimaryExpression
| '!' NotExpression
PrimaryExpression: Identifier
| Literal
| '(' Expression ')'
Literal: LITERAL
Identifier: IDENTIFIER
%%
There's probably a better (there's definitely a more concise) description of this, but I learned how to do this from this tutorial many years ago:
http://compilers.iecc.com/crenshaw/
It's a very easy read for non-programmers too (like me). You'll need only the first few chapters.

What does it mean when an array name is in parentheses with a caret pointer before it?

I have an array of unsigned numbers, called dataArray (actually in the program I am trying to figure out, they are entered as hexadecimal numbers, but I don't think that's important). I have another variable of type unsigned char, called c.
What does this do?
unsigned int dataArray[]={1,2,3,4,5};
unsigned char c;
x=c^ (dataArray)[i];
I have read that the caret is a reference to c, but what does it mean when the array name is in parentheses? It seems that x is just set to the (i-1)th element in dataArray, but under what conditions is that not so?
Thanks.
The parenthesis don't impact the semantics here. x is computed as the bitwise exclusive-or of c (undefined here) and dataArray[i].
In some derived language, the caret is used to declare a managed pointer. It's the so-called C++/CLI language made by microsoft and is used in .Net. It doesn't have that meaning in normal C++. In expressions, the caret means, as already explained by others, the bit-wise XOR, anyway.
The ^ character as it is used here is a bitwise XOR (see wikipedia). The parentheses don't to anything here, as the operator [] has higher precedence than bitwise-xor.
If this is really C++, then the '^' is the XOR bitwise operator. The parentheses are extra and do nothing in this example.
The ^ is not overloaded, as far as I know. If it is entered and compiles properly, it is going to attempt a XOR operation, though in this case it's probably going to be messing with data you don't want it to...
Ok, cool. That makes sense in the program I'm using. I had thought the caret was a pointer, but it's an XOR and then later on in the line there's an & (bitwise AND). The program is for sending and receiving signals from electronics, so bit-level logic is to be expected. Thanks very much for the fast replies.