-2
unsigned char setBit(unsigned char ch, int n)
{
    unsigned char mask = 1 << n;
    return ch | mask;
} 

I want to change n-bit with 1 in c# but I can't do it. I have only c++ examples.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Meppo
  • 23
  • 3
  • 4
    Stack Overflow is a Q&A site, not a code translation service. Try to translate the code yourself first, then come to us when you are stuck, making sure to show us what you have tried and create a [mre]. – gunr2171 Mar 18 '21 at 13:15
  • I think you'd cast the `char` to `int` then do the bit logic on that and cast back to `char`? – juharr Mar 18 '21 at 13:15
  • The bit shift operator is the same https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#left-shift-operator- – Self Mar 18 '21 at 13:17
  • And related https://stackoverflow.com/questions/23005206/which-datatype-is-used-for-unsigned-char-in-c – Self Mar 18 '21 at 13:17
  • 1
    Just change `unsigned char` to `byte`, all other seems same. – Alexey Rumyantsev Mar 18 '21 at 13:18
  • What have you tried in C#? Where are you stuck? – R Sahu Mar 18 '21 at 13:18
  • 4
    Does this answer your question? [Which datatype is used for unsigned char in C#?](https://stackoverflow.com/questions/23005206/which-datatype-is-used-for-unsigned-char-in-c). The only difference beeing unsigned char is byte in C# . – Self Mar 18 '21 at 13:22
  • 2
    Doing line-by-line translations from a language you know to one you don't know is doomed for failure at some point. The way you "translate" is to know both the source and destination languages well enough to understand what the source language is doing, and then basically throw away the source language version and implement the same thing in the destination language. – PaulMcKenzie Mar 18 '21 at 13:25
  • I've tried method static bool setBit (byte b, int pos) which returns b | (1 << pos) == 0; but it's wrong. It says operator "|" cannot be applied to operands of type 'byte' and 'bool'. – Meppo Mar 18 '21 at 13:29
  • 1
    @Denica I think your problem with that line is that the == takes higher precedence than the |. Try brackets around the b | (1 << pos) – Giles Mar 18 '21 at 13:33
  • 1
    Why `== 0` ? Isn't this what you want: https://dotnetfiddle.net/21Mkln ? – Fildor Mar 18 '21 at 13:41

1 Answers1

3

In C++ the char data type is one byte. In C# the equivalent type is byte.

In C# the char data type is a Unicode UTF-16 character (2 bytes).

byte setBit (byte b, int pos)
{
    return (byte)(b | 1 << pos - 1);
}
Giles
  • 1,331
  • 1
  • 15
  • 30
  • @gunr2171 I think you've misread. I said the C++ char data type in one byte. – Giles Mar 18 '21 at 13:20
  • 2
    @gunr2171 to be fair, Giles does say "In C# the char data type is a Unicode UTF-16 character." - the key bit is before that: "In C# the equivalent type is byte"; it would be nice if the answer actually addressed *how* to solve the question in the question, though – Marc Gravell Mar 18 '21 at 13:21
  • "char data type is one byte" - yes that is correct. But CHAR_BIT is not necessarily 8, although it probably is on any machine that runs the C# virtual machine. In general therefore `char` is not the same as the C# `byte`. – Bathsheba Mar 18 '21 at 13:59
  • 1
    ...that said, this is the correct way accomplish the task in C#. Have an upvote. – Bathsheba Mar 18 '21 at 14:14
  • I found similar question in which I found the answer for mine. It's this one https://stackoverflow.com/questions/24250582/set-a-specific-bit-in-an-int .. I don't know why I couldn't find it before. Sorry and thank you for your time everyone. – Meppo Mar 18 '21 at 15:43