Trying to understand the code in Delphi:

var r: byte; rbuf: array[0..1024] of Byte; begin ... r := rbuf[3] and 1; ... end 

Tell me which operator performs the same actions as the "And" operator but in C #.

  • four
    Obviously, the & operator. - VladD
  • 7
    By the way, your code has a strange buffer of 1025 bytes in length. - VladD
  • Well, why strange. Zero byte, sometimes, is needed for something, parity, criterion, belonging, etc. - Viktor Tomilov
  • 2
    @ViktorTomilov: You forgot the most frequent reason: you just forgot to subtract a unit, and this leads to very strange bugs. - VladD

2 answers 2

There are two types and :

  1. Boolean operator : in this case, the operands must be of type boolean
  2. Bit operator : in this case, the operands must be of type integer

C # uses its own operator for each case:

  1. Conditional operator AND : &&
  2. The logical operator AND : &

Since the question uses integers as operands, the analogous code in C # would be: rbuf[3] & 1;

  • @AndreyNOP, what do you mean by conditional in this case? The fact that he and bool works or what? - Grundy
  • No, I mean nothing. Just the answer is not clear. For each case, use its own operator: - What does this mean? It is assumed that cases are listed after the colon and what is used in them, but this is not the case ... - Andrew NOP
  • @AndreyNOP, it was an analogy with delphi, where the and operator can have different applications depending on the type of operands, in contrast, in C #, how the calculation will occur is determined by the operator, and not by the types of operands - Grundy
  • Well, Duc types of operands are also defined by the same (in C #), if we have bool operands, then & will be as boolean И , and not as bitwise - Andrey NOP
  • @AndreyNOP, not quite, what is the difference between boolean AND bitwise as applied to bool? - Grundy

The & operator in C # performs the same actions as the and operator in Delphi.