0

I have a short value X:

short X=1;  //Result in binary: 0000000000000001 
  1. I need to split them into an array and set the bits (say bit 6 and 10) //Result in binary: 0000001000100001
  2. I need to convert it back to short X value.

How can I do it painlessly? Could you please help?

3
  • Setting bits is a simple enough process, but I don't see the pattern behind retrieving the original short value afterwards. (Unless you will always be setting the 6th and 10th bit.) I'm also not sure what you mean by "splitting them into an array".
    – Abion47
    May 12, 2017 at 10:32
  • Splitting into arrays is only a suggesion. do you have a nice aproch in mind how to solv ethis? I am a newbie
    – Esmael
    May 12, 2017 at 10:36
  • So do you need to be able to convert the post-changed short back to the original short or not?
    – Abion47
    May 12, 2017 at 11:12

4 Answers 4

3

1. Manual solution

Setting bit 6 and 10:

myValue |= (1 << 6)|(1 << 10);

Clearing bit 6 and 10:

myValue &= ~((1 << 6)|(1 << 10));

2. Use BitArray

var bits = new BitArray(16); // 16 bits
bits[5] = true;
bits[10] = true;

Convert back to short:

var raw = new byte[2];
bits.CopyTo(raw, 0);
var asShort = BitConverter.ToInt16(raw, 0);
0

If what you are referring to is a very basic encryption, then perhaps using the XOR (^) operator would be better suited for your needs.

short FlipBytes(short original, params int[] bytesToSet)
{
    int key = 0;
    foreach (int b in bytesToSet)
    {
        if (b >= 0 && b < 16)
        {
            key |= 1 << b;
        }
    }

    return (short)(original ^ key);
}

This method will both set and reset the bytes that you desire. For example:

short X = 1;
short XEncrypt = FlipBytes(X, 6, 10);
short XDecrypt = FlipBytes(XEncrypt, 6, 10);

// X        = 1    , Binary = 0000000000000001
// XEncrypt = 1089 , Binary = 0000010001000001
// XDecrypt = 1    , Binary = 0000000000000001
3
  • @MongZhu I set the bits 6 and 10 if the first bit is regarded as bit 0. Presumably, OP regarded his first bit as bit 1. And regardless, that was an example that he gave, not a requirement. This still does the job that he needs.
    – Abion47
    May 12, 2017 at 18:35
  • @MongZhu I would say that bits are usually labeled starting as bit 0, because it lines up with the powers of 2 that they represent. (i.e. 2^0 = 1, 2^1 = 2, 2^2 = 4, etc.)
    – Abion47
    May 12, 2017 at 18:46
  • @Abion47 ok, you are right, the numbering of bits starts with zero, so OP numbered it wrong comparing to his expected output.
    – Mong Zhu
    May 15, 2017 at 8:56
0

If you have a int value "intValue" and you want to set a specific bit at position "bitPosition", do something like:

intValue = intValue | (1 << bitPosition);

or shorter:

intValue |= 1 << bitPosition;

If you want to reset a bit (i.e, set it to zero), you can do this:

intValue &= ~(1 << bitPosition);

(The operator ~ reverses each bit in a value, thus ~(1 << bitPosition) will result in an int where every bit is 1 except the bit at the given bitPosition.)

0

Linq solution, terser, but probably, less readable than foreach loop:

  using System.Linq;

  ...

  short X = 1;
  var bitsToSet = new[] { 5, 9 };

  var result = X | bitsToSet.Aggregate((s, a) => s |= 1 << a);

If you insist on short add cast:

  short result = (short) (X | bitsToSet.Aggregate((s, a) => s |= 1 << a));
2
  • This could be simplified by using the overload of Aggregate and setting X to be the initial seed. e.g. bitsToSet.Aggregate(X, (s, a) => s |= 1 << a);
    – Abion47
    May 12, 2017 at 11:10
  • @Abion47: X is of type short when 1 << a is of type int, that's why we have to add cast: bitsToSet.Aggregate(X, (s, a) => s |= (short) (1 << a)); May 12, 2017 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.