Questions tagged [complement]

This tag is deprecated! Use more specific tags instead: For the binary representations of negative values in computer science, use tags [ones-complement] and [twos-complement]; For the bitwise complement operation, use [bitwise], or [regex-negation] for the use of complement patterns in regular expressions, or [bit-manipulation] for complementing said data.

This tag is deprecated! Please consider use of the following tags instead:

112 questions
215
votes
19 answers

How does Python's bitwise complement operator (~ tilde) work?

Why is it that ~2 is equal to -3? How does ~ operator work?
bala
  • 2,231
  • 2
  • 14
  • 9
35
votes
6 answers

Need help understanding "getbits()" method in Chapter 2 of K&R C

In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works. Here's the method provided: unsigned int getbits(unsigned int x, int p, int n) { return (x >> (p + 1 - n)) & ~(~0…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
27
votes
3 answers

Quickest way to find the complement of two collections in C#

I have two collections of type ICollection called c1 and c2. I'd like to find the set of items that are in c2 that are not in c1, where the heuristic for equality is the Id property on MyType. What is the quickest way to perform this in C#…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
20
votes
7 answers

How can I invert bits of an unsigned byte in Java?

I am trying to write a decoder for a very simple type of encryption. Numbers from 0-255 are entered via Scanner, the bits are inverted, and then converted to a character and printed. For example, the number 178 should convert to the letter "M".…
DavidKelly999
  • 301
  • 1
  • 2
  • 5
19
votes
7 answers

Is two's complement notation of a positive number the same number?

Is two's complement notation of a positive number is same as its binary representation?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
19
votes
4 answers

Why is the complement operator not working when bool = true?

I have written this C++ program, and I am not able to understand why it is printing 1 in the third cout statement. #include using namespace std; int main() { bool b = false; cout << b << "\n"; // Print 0 b = ~b; cout <<…
vikiiii
  • 9,246
  • 9
  • 49
  • 68
10
votes
7 answers

Complement a DNA sequence

Suppose I have a DNA sequence. I want to get the complement of it. I used the following code but I am not getting it. What am I doing wrong…
Anurag Mishra
  • 1,007
  • 6
  • 16
  • 23
10
votes
3 answers

Iterate over (item, others) in a list

Suppose I have a list: l = [0, 1, 2, 3] How can I iterate over the list, taking each item along with its complement from the list? That is, for item, others in ... print(item, others) would print 0 [1, 2, 3] 1 [0, 2, 3] 2 [0, 1, 3] 3 [0, 1,…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
4
votes
4 answers

How to find 10's complement of decimal number

Whats the 10's complement of 1056.074? Please check the solution: 9999.999 - 1056.074 = 8943.925 (9's complement of 1056.074) Now 1 is added to .074 or 8943?
user5011938
4
votes
8 answers

why C, C++, Java does not use one complement?

I heard C, C++, Java uses two complements for binary representation. Why not use 1 complement? Is there any advantage to use 2 complement over 1 complement?
user188276
4
votes
2 answers

find complement of regular expression

There's a question on my exercise sheet to find the complement of two formulas (1) (aa|bb)* and (2) (a|b)(aa|bb)(a|b). complement of both is in my opinion a* | b*, meaning only a's or only b's?
sushi
  • 41
  • 1
  • 1
  • 2
3
votes
2 answers

Basic set-based operations using a document database (noSQL)

As with most, I come from and RDMS world trying to get my head around noSQL databases and specifically document stores (as I find them the most interesting). I am try to understand how to perform some set-based operations using a document database…
amok
  • 105
  • 5
3
votes
2 answers

Google spreadsheet get complements

Link here to spreadsheet As the title says, how can I end up with cells that don't match in value, the difference/complement (set theory)? I would prefer it with only FUNCTIONS and no script. {1,2,3,4}\{1,3} = {2, 4} or with letters {a,b,c,d}\{a,c}…
KaZyKa
  • 325
  • 2
  • 10
3
votes
1 answer

Complement Graph - prolog

I have a question. I have a continuous undirected graph. So and I need a code in Prolog which give me a complementary graph. for example graph: edge(1,2). edge(2,3). edge(3,4). edge(4,5). edge(5,1). rot(X,Y):- edge(X,Y). rot(X,Y):-…
3
votes
1 answer

Finding complement date ranges?

I have two tables both of which have columns StartDate and EndDate. I'm trying to return a single resultset that contains all date ranges from one table (TableA), and all complement date ranges from the other one (TableB). CREATE TABLE…
1
2 3 4 5 6 7 8