Using bitwise operators

I am learning C # and am on familiar ground from my old work in C ++. I never understood the reasons for bitwise operators in a real application. I never used them and never had a reason to use them. I study how they work; the example below shows a bitwise shift operator. What is the point of bitwise operators, their use and how do they work?

Maybe I lost something in bitwise logic.

byte bitComp = 15;              // bitComp = 15 = 00001111b
byte bresult = (byte) ~bitComp; // bresult = 240 = 11110000b

Here is an example for the bitwise complement operator ~:

byte bitComp = 15;              // bitComp = 15 = 00001111b
byte bresult = (byte) ~bitComp; // bresult = 240 = 11110000b
+3
source share
14 answers

A typical use is the use of bits, which are mutually exclusive flags.

MSDN:

[Flags]
enum Days2
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

class MyClass
{
    Days2 meetingDays = Days2.Tuesday | Days2.Thursday;

    Days2 notWednesday = ~(Days2.Wednesday);
}

. Stack #.

+17

, :

, , , :

enum
{
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Option4 = 8,
    Option5 = 16
};

, . , :

enum
{
    Option1 = 1<<0,
    Option2 = 1<<1,
    Option3 = 1<<2,
    Option4 = 1<<3,
    Option5 = 1<<4
};
+8

(, , ) . RGB:

#define RGB(r, g ,b)  ((DWORD) (((BYTE) (r) | ((WORD) (g) << 8)) | (((DWORD) (BYTE) (b)) << 16)))

3 , RGB.

+5

, , - . , , (, 128). ( , / - , ):

    public static bool TryDecodeUInt32(Stream source, out uint value)
    {
        if (source == null) throw new ArgumentNullException("source");

        int b = source.ReadByte();
        if (b < 0)
        {
            value = 0;
            return false;
        }

        if ((b & 0x80) == 0)
        {
            // single-byte
            value = (uint) b;
            return true;
        }

        int shift = 7;

        value = (uint)(b & 0x7F);
        bool keepGoing;
        int i = 0;
        do
        {
            b = source.ReadByte();
            if (b < 0) throw new EndOfStreamException();
            i++;
            keepGoing = (b & 0x80) != 0;
            value |= ((uint)(b & 0x7F)) << shift;
            shift += 7;
        } while (keepGoing && i < 4);
        if (keepGoing && i == 4)
        {
            throw new OverflowException();
        }
        return true;
    }

:

  • , ( )
  • --

, ( ) . .

. .

( ..), .

+4

COM:

. HRESULT - , 32- . - , , (0) (1). 15 , , - ole win32 - . 16 .

, HRESULT .

. ( C, ), , HRESULT , (hr 0x80000000)!= 0 , . .

, .

+2

: "

1) , (, ). ++, #.

2) , CRC. . - " ", , .

3) , , ( ) , . , , , , . Endianness. , " " .

+1

:

  • : , , , -, , . , .

  • : , ( ) .

, C ++ , , . , .

+1

", ": - , , NAND NOR, - NAND. , , , , , , .

"": , , ops, . , , , , , , RAID5 , , CRC, , .

, , fd_set, syscall select /.

MPEG4, , .

+1

(< → ) , , , .

, , 5*2^7. :

int result = 5 * (int)Math.Pow(2, 7);

, :

int result = 5 << 7;

.

+1

, , . :

bool data[64] = {...};

64 (512 ) . ​​ 8 (64 ) :

uint64_t data = ...;

, , DRAM, , , - , , 64- , - 64- , , , . , , 1/8 , .

, - , :

enum Flags
{
     flag_selected =  1 << 0,
     flag_hidden =    1 << 1,
     flag_removed =   1 << 2,
     flag_hovering =  1 << 3,
     flag_minimized = 1 << 4,
     ...
};
uint8_t flags = flag_selected | flag_hovering;

, , , :

// Check if the element is hidden or removed.
if (flags & (flag_hidden | flag_removed))
{
    ...
}

bitwise and, flag_hidden flag_removed , .

:

bool data[64];

, , 64 , - . , :

bool all_set = true;
for (int j=0; j < 64; ++j)
{
     if (!data[j])
     {
          all_set = false;
          break;
     }
}
if (all_set)
{
     // Do something different when all booleans are set.
     ...
}

, :

uint64_t data = ...;
if (data == 0xffffffffffffffff)
{
     // Do something different when all bits are set.
     ...
}

64 , 64- . SIMD 64 SIMD.

, , . , , :

int count = 0;
for (int j=0; j < 64; ++j)
     count += data[j];
// do something with count

, , :

uint64_t data = ...;
const int count =  __popcnt64(data);
// do something with count

. , , 64 ​​ , true.

. - :

x = pow(2, n);

n - , :

x = 1 << n;

, , intrinsics pow, , , , C ++, , , , n .

, , . , :

x = n % power_of_two;

... power_of_two - , . :

x = n & (power_of_two - 1);

, ( ). , , . , 16 0b10000. , : 0b1111 , n % 16.

, .. , , 16x16, 32x32, 64x64, 256x256 .. , .

, , , , . .

- . , voxel Atomontage, , , DRAM, , , . , , 8 true/false, .

+1

( ), , , . x , y, , z, .. .

0

, : cool-iness!;)

, , :

int abs(const int input) {
   int temp = A >> 31;
   return ( input ^ A ) - A;
}

, , , , .

, , .

0

. , , . :

000000000000000000000000000001100100

int result = 5 & 3;
Console.WriteLine(result);


5:  00000000000000000000000000000101
3:  00000000000000000000000000000011
  ------------------------------------
1:  00000000000000000000000000000001

. int 32 , 0, . AND . , .

0

:

XOR

AND|0 1        OR|0 1 
---+----      ---+---- 
  0|0 0         0|0 1 
  1|0 1         1|1 1 

XOR | 0 1 | 0 1 --- + ---- --- + --- 0 | 0 1 | 1 0 1 | 1 0 .

203: 1100 1011

15: 0000 1111

= 11: 0000 1011

x * 2y . .

int main()
{
     int x = 19;
     printf ("x << 1 = %d\n" , x <<1);
     printf ("x >> 1 = %d\n", x >>1);
     return 0;
}
// Output: 38 9
The & operator can be used to quickly check if a number is odd or even
Eg.

int main()
{
    int x = 19;
    (x & 1)? printf("Odd"): printf("Even");
    return 0;
 }
// Output: Odd
Quick find minimum of x and y without if else statment
Eg.

int min(int x, int y)
{
    return y ^ ((x ^ y) & - (x < y))
}

. .

#include <stdio.h>
int main ()
{
    int n , c , k ;
    printf("Enter an integer in decimal number system\n " ) ;
    scanf( "%d" , & n );
    printf("%d in binary number
    system is: \n " , n ) ;
    for ( c = 31; c >= 0 ; c -- )
    {
         k = n >> c ;
         if ( k & 1 )
              printf("1" ) ;
         else
              printf("0" ) ;
      }
      printf(" \n " );
      return 0 ;
}

There is also a wide range of uses for bitwise logic.

0
source

Source: https://habr.com/ru/post/1720384/


All Articles