Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What are the uses of Bitwise operators in real life?
There were many cases where so many questions are solved by using bitwise operators.

especially when >> and & is used ?

For example:

I have seen this code for a particular question :

C++
void Usort(T *a, int n) {
	int i, k;
#define B 8
#define BMSK 255 // (1<<8)-1
	int t1[1 << B], t2[1 << B];

	T *tmp = malloc(n * sizeof(T));
	for (k = 0; k < 4; ++k) {  // for 32ビット
		memset(t1, 0, sizeof(t1)), memset(t2, 0, sizeof(t2));
		for (i = 0; i < n; ++i)	t1[(a[i].x >> k*B) & BMSK]++;
		for (i = 0; i < BMSK; ++i) t1[i+1] += t1[i];
		i = n; while (i--) tmp[--t1[(a[i].x >> k*B) & BMSK]] = a[i];
		k++;
		for (i = 0; i < n; ++i) t2[(tmp[i].x >> k*B) & BMSK]++;
		for (i = 0; i < BMSK; ++i) t2[i+1] += t2[i];
		i = n; while (i--) a[--t2[(tmp[i].x >> k*B) & BMSK]] = tmp[i];
	}
	free(tmp);
#undef B
// for (i = 0; i < n; ++i) printf("[%d] %lld\n", i, a[i]);
}


What I have tried:

I searched in google,
But the matter is up on what are bitwise operators...
I am not getting any information about where we use bitwise operators & and << in problems and real life.
Posted
Updated 3-Aug-21 3:07am
Comments
Patrice T 3-Aug-21 1:50am    
Why don't you ask the author what this code is doing ?
_-_-_-me 3-Aug-21 2:01am    
But I don't know who the author is
I saw this code in submitted answers section
Thank you !

They are typically used when single bits of an integer have independent meaning.
For a 'real life' example see the uFlags parameter of the SetWindowPos function (Winapi):
SetWindowPos function (winuser.h) - Win32 apps | Microsoft Docs[^].
 
Share this answer
 
Comments
_-_-_-me 3-Aug-21 2:04am    
Okay , thank you!
In the real world? You use them every single day, and just probably don't realize it.
In fact, by reading this text you are using them right now!

How do I know? Because Codeproject is an HTTPS site - which means it uses an encrypted connection between your browser and the CodeProject server. Encryption is heavily based on bitwise operations!

Same thing for ZIP and RAR files - if you use any compression software, that's all bitwise "under the hood".

Got a RAID5 drive? I have two under my desk - they are bitwise as well: the simplest form is to split each byte in two, store one nibble on one disk, the other nibble on the second disk, and the XOR of the two on a third. If any one disk fails, you can use the info on the other two to regenerate the complete data.

The internet use bitwise operations: most electronic communications do!
You satelite TV does, your phone does, every password you enter (except those developed by incompetent developers) does.

Just converting from upper to lower case characters and vice versa does!
 
Share this answer
 
Comments
_-_-_-me 3-Aug-21 2:07am    
Okay , thank you !
The conclusion is : Its pretty much used everywhere. So, one have to understand how the bit wise operators work :-)
Here are some uses of them. These are functions to test, set, and clear bits within a byte.
C++
typedef unsigned char  UCHAR;

// set a bit - generate a mask by shifting 1 to the desired position
// then OR the mask with the input value to set the requested bit.

UCHAR BitSet( UCHAR a, int bit )
{
	UCHAR mask = 1 << bit;
	return a | mask;
}

// clear a bit - generate a mask by shifting 1 to the desired position,
// get the complement of the mask and then AND the mask with the input
// value to clear the requested bit.

UCHAR BitClear( UCHAR a, int bit )
{
	UCHAR mask = ~( 1 << bit );
	return a & mask;
}

// test a bit - generate a mask by shifting 1 to the desired position,
// then AND the mask with the input value.  If the bit was set then the
// result will be non-zero and it will be 0 otherwise.

int BitTest( UCHAR a, int bit )
{
	UCHAR mask = 1 << bit;
	return ( a & mask ) ? 1 : 0;
}
I find that I use these functions most often in code involving communication handling used between devices on ethernet or serial (RS-232 or RS-485) interfaces. The machines my code talks to have a lot of bit-banging in their protocols. These are things like PLCs, LASER scanners, smart cameras, motion controllers, and others using protocols like ModBus, SECS, and proprietary ones.

These functions can be templatized easily by changing UCHAR to T and adding the template< typename T > keywords.
 
Share this answer
 
Comments
_-_-_-me 3-Aug-21 2:22am    
Thank you very much !
set a bit - generate a mask by shifting 1 to the desired position
- Here- what is desired position?
I understood it like this:
In a case to increase bit size.
Suppose when you want to store 4 bit in an 8 bit .
Is my understanding correct?
If my understanding is correct:
Why one have to increase the size , it would lead to more memmory wastage .

Thank you !
Rick York 3-Aug-21 11:41am    
I doesn't really work that way. Storage is in units of bytes so if you only use three bits there is still a byte stored.

When shifting to make the mask the desired position is the "bit" parameter that was passed and it can range from 0 to 7.
_-_-_-me 4-Aug-21 6:56am    
Okay , Thank you !
please , could you explain the meaning of mask ?
Here what is mask ?
Is it the one which we are shifting ?
Thank you !
CPallini 3-Aug-21 2:23am    
In C programming language, templatizing them it is not that easy. :-)
Rick York 3-Aug-21 11:38am    
True. Templates are not particularly useful with the C language but if you think of them as essentially smart macros they can be adapted.
Quote:
What are the uses of Bitwise operators in real life?

Here is a real life example of use of bitwise operations : Integer Factorization: Dreaded List of Primes[^]
In this article, I use a table to store a compressed list of prime integers and bitwise operations are used to access the table.
 
Share this answer
 
Comments
_-_-_-me 3-Aug-21 9:30am    
Thank you very much :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900