| |

VerySource

 Forgot password?
 Register
Search
View: 1180|Reply: 8

Eagerly hope: "&" "|=" "^=" What are the practical meanings of these bit operators

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-16 20:30:02
| Show all posts |Read mode
Recently I was reading the original code, but I don’t understand the bitwise operators very clearly. Please help me explain the practical meaning of the bitwise operators "&", "|=" and "^=". It is best to give Thanks for the specific application example
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-12-17 02:30:01
| Show all posts
&is bitwise and
|=Bitwise OR first, then assignment.
^=Bitwise XOR first, then assignment.
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Unknown

Post time: 2020-12-17 08:00:01
| Show all posts
In network programming, when you define a frame in communication with others, for example, the frame header is 0xFF, how can you define it as the frame header without bit manipulation?
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-17 16:45:01
| Show all posts
&is bitwise and 1&1==1 0&1==0 1&0==0 0&0==0
|Is bitwise OR 1|1==1 0|1==1 1|0==1 0|0==0
^is bitwise XOR 1^1==0 0^1==1 1^0==1 0^0==0
|=Bitwise OR first, then assignment.
^=Bitwise XOR first, then assignment.
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 United States

Post time: 2020-12-18 21:00:01
| Show all posts
If you don’t know what is the use of bitwise operations, or don’t know what bitwise operations are, you can read some books on electronic circuits or assembly. Bitwise operations are often required for hardware or communication;
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-19 19:00:02
| Show all posts
BOOL CFileView::PreCreateWindow(CREATESTRUCT&cs)
  {
  if (!CListView::PreCreateWindow (cs))
  return FALSE;
   
          cs.style&= ~LVS_TYPEMASK;
          cs.style |= LVS_REPORT;
  return TRUE;
  }

Someone explained
cs.style&= ~LVS_TYPEMASK;//Get the window or control style LVS_TYPEMASK
cs.style |= LVS_REPORT; //Increase window or control style LVS_REPORT
What does this mean, can you explain it in detail, and at the same time:
bool
HeapTupleSatisfiesItself(HeapTupleHeader tuple, Buffer buffer)
{
if (!(tuple->t_infomask&HEAP_XMIN_COMMITTED))//tuple->t_infomask and HEAP_XMIN_COMMITTED
{
if (tuple->t_infomask&HEAP_XMIN_INVALID)
return false;

if (tuple->t_infomask&HEAP_MOVED_OFF)
{
TransactionId xvac = HeapTupleHeaderGetXvac(tuple);

if (TransactionIdIsCurrentTransactionId(xvac))
return false;
if (!TransactionIdIsInProgress(xvac))
{
if (TransactionIdDidCommit(xvac))
{
tuple->t_infomask |= HEAP_XMIN_INVALID;
SetBufferCommitInfoNeedsSave(buffer);
return false;
}
tuple->t_infomask |= HEAP_XMIN_COMMITTED;
SetBufferCommitInfoNeedsSave(buffer);
}
}
else if (tuple->t_infomask&HEAP_MOVED_IN)
{
TransactionId xvac = HeapTupleHeaderGetXvac(tuple);

if (!TransactionIdIsCurrentTransactionId(xvac))
{
if (TransactionIdIsInProgress(xvac))
return false;
if (TransactionIdDidCommit(xvac))
{
tuple->t_infomask |= HEAP_XMIN_COMMITTED;
SetBufferCommitInfoNeedsSave(buffer);
}
else
{
tuple->t_infomask |= HEAP_XMIN_INVALID;
SetBufferCommitInfoNeedsSave(buffer);
return false;
}
}
}
else if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(tuple)))
{
if (tuple->t_infomask&HEAP_XMAX_INVALID) /* xid invalid */
return true;

/* deleting subtransaction aborted */
if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
{
tuple->t_infomask |= HEAP_XMAX_INVALID;
SetBufferCommitInfoNeedsSave(buffer);
return true;
}

Assert(TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)));

if (tuple->t_infomask&HEAP_MARKED_FOR_UPDATE)
return true;

return false;
}
else if (!TransactionIdDidCommit(HeapTupleHeaderGetXmin(tuple)))
{
if (TransactionIdDidAbort(HeapTupleHeaderGetXmin(tuple)))
{
tuple->t_infomask |= HEAP_XMIN_INVALID; //tuple->//
SetBufferCommitInfoNeedsSave(buffer);
}
return false;
}
else
{
tuple->t_infomask |= HEAP_XMIN_COMMITTED;
SetBufferCommitInfoNeedsSave(buffer);
}
}

/* by here, the inserting transaction has committed */

if (tuple->t_infomask&HEAP_XMAX_INVALID) /* xid invalid or aborted */
return true;

if (tuple->t_infomask&HEAP_XMAX_COMMITTED)
{
if (tuple->t_infomask&HEAP_MARKED_FOR_UPDATE)
return true;
return false; /* updated by other */
}

if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(tuple)))
{
if (tuple->t_infomask&HEAP_MARKED_FOR_UPDATE)
return true;
return false;
}

if (!TransactionIdDidCommit(HeapTupleHeaderGetXmax(tuple)))
{
if (TransactionIdDidAbort(HeapTupleHeaderGetXmax(tuple)))
{
tuple->t_infomask |= HEAP_XMAX_INVALID;
SetBufferCommitInfoNeedsSave(buffer);
}
return true;
}

/* xmax transaction committed */

if (tuple->t_infomask&HEAP_MARKED_FOR_UPDATE)
{
tuple->t_infomask |= HEAP_XMAX_INVALID;
SetBufferCommitInfoNeedsSave(buffer);
return true;
}

tuple->t_infomask |= HEAP_XMAX_COMMITTED;
SetBufferCommitInfoNeedsSave(buffer);
return false;
}
Reply

Use magic Report

0

Threads

45

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-12-19 21:45:01
| Show all posts
cs.style&= ~LVS_TYPEMASK;//Get the window or control style LVS_TYPEMASK
cs.style |= LVS_REPORT; //Increase window or control style LVS_REPORT

Isn't the comment already clear?

Like LVS_REPORT are defined constants, for example 4, now cs.style is 2.

You can use a formula to explain
2|4=6
The above formula is converted to binary
010|100=110


In other words, cs.style is now 6
When the function is passed in, the function implementation will be split, the method can be&~ which is the inverse operation of the OR operation, to determine whether there is this option LVS_REPORT setting
The values ​​of these constants are all designed. Any two of the constants or operations have only unique values.
In the function implementation, you can judge based on these values
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-12-20 00:15:01
| Show all posts
&and, two are 1 is 1, the others are 0
| Or, two 0 is 0, the other is 1
^ Or, two identical are 0, two different are 1
&= a&= b; is equal to a = a&b;
|= as above
^= as above

skill:
And, it can be used to mask out some bits, or to reserve some bits, with a number, the zero bit can be used to mask, and 1 bit can be used to keep the original state
0xffff&0x00ff == 0x00ff, mask the high bit 0xff00, reserve the low bit 0x00ff;
Or, it can be used to set some bits, because the original 1 bit will not be changed, only the zero bit can be changed, and multiple settings can be combined.
0x000f | 0x00f0 == 0x00ff;
Then you can use and to determine whether this bit is set
0x00ff&ox00f0 == 0x00f0;
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Great Britain

Post time: 2021-3-12 17:08:40
| Show all posts
Think of ! as a thing that inverts all the bits
0 -> 1
1 -> 0
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list