Questions tagged [bitwise-not]

24 questions
7
votes
3 answers

Is ~i really equivalent to i != -1?

How does ~i work in C++? I just noticed it is equivalent to i != -1, but I'm not certain about that. int arr[3] {1, 2, 3}; int n = 3; for (int i = n - 1; ~i; i--) { cout << arr[i] << ' '; } It printed the array in reverse.
4
votes
1 answer

OpenCV(4.0.0) Python Error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'

I am trying to apply mask on an image using opencv bitwise-not. I am able to achieve this result if I read both original and mask image in Greyscale mode, but it doesn't work on 3 channel images. I have read this thread OpenCV Python Error: error:…
4
votes
3 answers

How does bitwise not operation give negative value

I want to see how bitwise NOT works through a simple example: int x = 4; int y; int z; y = ~(x<<1); z =~(0x01<<1); cout<<"y = "<
jwm
  • 4,832
  • 10
  • 46
  • 78
3
votes
1 answer

How do I write NOT Operation for the Risc-V (Assembly Language)?

How do I write NOT Operation for the Risc-V (Assembly Language)? If there's no NOT instruction, how do you achieve the same thing?
3
votes
0 answers

Why does ~~Infinity return 0?

Recently I have been playing around with the double bitwise not (~~) operator in JavaScript. I noticed that ~~Infinity returns 0. Why is this? I would expect it to return Infinity, similarly to how Math.trunc(Infinity) returns Infinity and not…
Shnick
  • 1,199
  • 1
  • 13
  • 32
3
votes
3 answers

(Not 1) evaluates to -2 for some reason

Why does (Not 1) evaluate as -2? I would expect it to evaluate as 0.
public wireless
  • 767
  • 8
  • 20
2
votes
1 answer

What is the exact definition of bitwise not in Python, given arbitrary length integers?

Bitwise not (~) is well-defined in languages that define a specific bit length and format for ints. Since in Python 3, ints can be any length, they by definition have variable number of bits. Internally, I believe Python uses at least 28 bytes to…
SRobertJames
  • 8,210
  • 14
  • 60
  • 107
2
votes
1 answer

Verilog: Why can I not invert a wire?

module hamming_code #( parameter TOTAL_LENGTH = 15, parameter PARITY_BITS = 4 ) ( //inputs input [TOTAL_LENGTH-1:0] codeword, …
Faytll
  • 87
  • 2
  • 12
1
vote
1 answer

Bitwise not operator doesn't flip bits

I am trying to use the bitwise not operator to flip the bits in 1. I start with 1 (00000001) and add ~. x = ~1 print(f'{x:08b}') Result is -0000010. Everything online tells me the result should be 11111110. Not sure what I can do wrong with putting…
newbie54
  • 23
  • 4
1
vote
1 answer

Minimum amount of flips required to make binary strings match, under the restriction that there must never be three of the same bit consecutively

Imagine you have two binary strings, a and b, given as input. a and b are both of the same length. The objective is to find the minimum number of flips required to make a equal b, WITHOUT a ever having three of the same bit consecutively (e.g. 000…
1
vote
0 answers

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…
1
vote
0 answers

Bitwise NOT operation on integers

Why compiler does 2's complement in the bitwise NOT operation? example : ~5 = -6 ~5 = 1010 which is actually 10 in decimal but compiler takes 2's complement of 1010 which will be -6.
1
vote
1 answer

What is the effect of a double negating bitwise operator (~~) - also called "double tilde" - in PHP?

Refactoring legacy code I have found various occurrences of the following construct: ((bool) ~~$field->req ? ' required' : '') According to the manual the Tilde (~) in PHP is the bitwise not, which simple shifts all bits in a variable to their…
Blackbam
  • 17,496
  • 26
  • 97
  • 150
1
vote
1 answer

JavaScript bitwise NOT ~ doesn't produce same result

I am attempting to invert the binary digits of a base-10 value using JavaScript, according to this W3Schools page, JavaScript's Bitwise NOT operator (i.e. ~) should do just that. In an example they show ~5 resulting in a value of 10, but when I…
Brendan Whiting
  • 494
  • 3
  • 13
1
vote
2 answers

Bitwise negation of XMM register

How to get bitwise negation of values in XMM register? As far as I know there is no such instruction. The only instruction with negation is pandn, but to use it to simply negate values in one XMM register, I would have to have another XMM register…
Rames
  • 918
  • 11
  • 27
1
2