What is the purpose of "int mask = ~0;"?

I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1. So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0? Can we use -1 i...
more »

2017-09-23 08:09 (6) Answers

Bitwise operation in C to compare two integers

I was recently given a quiz in one of my classes. The question is below: Write a function (called cmp) in C that accepts two integers (x and y) and returns: -1 if x < y, 0 if x = y, 1 if x > y. Write cmp as concise as possible. The most co...
more »

2017-09-10 01:09 (3) Answers

Bit manipulation and Flags testing in C

Suppose I have two variables of type int, a and b, and a flag F. #define F <something> int a = <something> ; int b = <something> ; What is a simple way to test that both a and b, have the flag F, or none of them has it? To te...
more »

2017-08-19 15:08 (3) Answers

How I can remove all odds bits in C?

I have int 136970250 (1000 0010 1010 0000 0000 0000 1010) -> I need to remove all odd bits(1, 3, 5, 7...) 1000 0010 1010 0000 0000 0000 1010 -> 10 0111 0000 0011 (9987) - because this bits do not have information. How to do it? ...
more »

2017-08-15 16:08 (3) Answers

Shifting with uint64_t not working as expected

For part of a larger task, I was asked to implement a function that flips an arbitrary bit in an integer. The catch is that the "integer" could be any of the default integer types in c, from int8_t to uint64_t, and I don't know which one it will be. ...
more »

2017-08-09 22:08 (1) Answers

The index of the first bit

I can see in http://hackage.haskell.org/package/base- how to convert from an Int, n, to a Bit that has the nth bit set, using bit :: Int -> a However, how can I do the inverse of this? (Assuming the input Bit ju...
more »

2017-08-08 23:08 (2) Answers

Reversing the bits of an integer in C

I am trying to reverse the bits of an integer in the C program. Even though I have looked at the same question by another user, I was unable to understand most of the code that was written. I have noticed that the code I have is similar to the answe...
more »

2017-08-01 06:08 (2) Answers

How does & bit operator work here?

In Java Collection classes, I have noticed very often codes like below //ArrayDeque public E pollFirst() { int h = head; @SuppressWarnings("unchecked") E result = (E) elements[h]; // Element is null if deque empty if (resu...
more »

2017-07-08 11:07 (2) Answers

Unusual behavior with shift-right bitwise operator

I'm writing a simple code in C (only using bit-wise operators) that takes a pointer to an unsigned integer x and flips the bit at the nth position n in the binary notation of the integer. The function is declared as follows: int flip_bit (unsigned ...
more »

2017-05-16 02:05 (3) Answers

Find most significant bit in Swift

I need to find the value (or position) of the most significant bit (MSB) of an integer in Swift. Eg: Input number: 9 Input as binary: 1001 MS value as binary: 1000 -> (which is 8 in decimal) MS position as decimal: 3 (because 1<<3 == 1000) ...
more »

2017-05-08 22:05 (1) Answers

How bit manipulation works?

There was a question asked: "Presented with the integer n, find the 0-based position of the second rightmost zero bit in its binary representation (it is guaranteed that such a bit exists), counting from right to left. Return the value o...
more »

2017-04-20 07:04 (1) Answers

Efficient way to split up RGB values in C

I'm writing some software for a 32-bit cortex M0 microcontroller in C and I'm doing alot of manipulations with 32-bit RGB values. They are handled in a 32-bit integer format like 0x00BBRRGG. I want to be able to do math with them without worrying abo...
more »

2017-04-18 05:04 (5) Answers

Bitwise Operations to change 2 LSB

Suppose I have an list of numbers: l = [30, 31, 32, 33] In binary this would the same as l = [00011110, 00011111, 00100000, 00100001] Using binary operations I want to set the least 2 significant bits to any random value, but retain the 6 most...
more »

2017-04-03 05:04 (2) Answers

Optimized way to find new bits on

We need to find new bits turned ON in interlock status received from the device compared to the last status read. This is for firing error codes for bits that are newly set. I am using the following statement. bits_on =~last_status & new_status;...
more »

2017-03-10 07:03 (1) Answers

Fastest Two Number Sign Comparison Including Zero

What is the fastest way to find if two numbers have the same sign considering that the sign can either be positive, negative or zero. Commonly, you can say two numbers have same signs with this: Math.signum(int1) == Math.signum(int2); You can opt...
more »

2017-02-07 22:02 (3) Answers

C-language binary shift operator

I'm trying to learn some basic C for a project. On the Wikipedia article on binary shift operators: If the variable ch contains the bit pattern 11100101, then ch >> 1 will produce the result 01110010, and ch >> 2 will produce 00111001. Just fo...
more »

2017-02-02 22:02 (3) Answers

Unable to convert a binary to decimal

My aim is to have a decimal which has same leading ones in binary as the user specifies, which is stored in the unsigned long variable mask; the remaining bits would be set to zero. int mask; scanf("%d",&mask); unsigned long subnetMask = 0; i...
more »

2017-01-25 02:01 (2) Answers

Extracting bits from uint8_t type array

I have the data: ef324ad13255e219e8110044997cefaa43ff0954800000000000007 stored in an uint8_t type array called lfsr[36]. I want to extract specific bits from the array, e.g. bit no. 96, bit no. 184 etc. How can I perform this operation? ...
more »

2016-12-12 19:12 (4) Answers