Solved

C# Bit Shifter

Posted on 2011-09-23
8
334 Views
Last Modified: 2013-12-17
How do you modify the Shifter method to move the bits to the appropriate locations?

Thx
using System;
using System.Diagnostics;

class Bitshifter
{
    // Bits   Description
    // 31:16  a
    // 15:8   b
    // 7:4    must be zero
    // 3:0    c
    // +----------------+--------+----+----+
    // |        a       |    b   | mbz| c  |
    // +----------------+--------+----+----+
    //  3              1 1
    //  1              6 5      8 7  4 3  0

    // Assembles a, b, and c into the single 32-bit register described above
    static UInt32 shifter(UInt32 a, UInt32 b, UInt32 c)
    {
        return 0;
    }

    static void Main(string[] args)
    {
        Debug.Assert(0x101070e == shifter(0x101, 7, 0xe));
        //Debug.Assert( 0x8101070e == shifter( 0x8101, 0xff07, 0xe ) );
        Console.WriteLine("Success!");
    }

}

Open in new window

0
Comment
Question by:CipherIS
8 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36592087
  Basic Solution:   return (((A << 15) | (B << 8)) | C);

But note that you need do Validations before:

    if (A > 0x10000)        throw new Exception("A Overflow");
    if (B > 0x100)            throw new Exception("b Overflow");
    if (C > 0x10)              throw new Exception("c Overflow");
    return (((A << 16) | (B << 8)) | C);
0
 
LVL 15

Expert Comment

by:x77
ID: 36592090
Sorry, validation  comparison must be  ">="
0
 
LVL 1

Author Comment

by:CipherIS
ID: 36592577
That is not the solution.  Please look at this - this may help.

    //Shift:
    //x = 1;
    //x = x << 1; // x is now 2  (00000010 binary, 0x02 hex)
    //x = x << 4; // x is now 32 (00100000 binary, 0x20 hex)

    //Bitwise OR:
    //00 | 00 = 00
    //00 | 01 = 01
    //11 | 10 = 11
    //11 | 11 = 11

    //Bitwise AND:
    //00 & 00 = 00
    //00 & 01 = 00
    //11 & 10 = 10
    //11 & 11 = 11
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36593994
Is this schoolwork?
0
 
LVL 15

Expert Comment

by:x77
ID: 36594055
About Shift operator.

There two Sift operator : Signed, Unsigned

When you wants put Bit 0-15 to position 16-31 you can use:  myvar = myVar << 16

On Unsigned, Bit 0-15 = 0
on Signed      Bit 0-15 = Sign bit. - for positive numbers identical to Unsigned.

To Put bit 31-16 to position 15-0:  myVar = myVar >> 16
Same considerations on Bits 31-16 as before on Bit 0-15.

I Put correct solution except first line (15 must be 16) as I put on last line.

As you can see solution is some more complex on signed values as you need aditional mask or cast to forze Unsigned Shift (note that cast can trhow exception when Checked, By default Csharp uses UnChecked).

As You says values and result is unsigned, I don´t put code to mask (and) result from Shift operation.
but I put code to verify Overflow on each bit area.

By sample, for  Signed values:

  Return  ((A << 16) & 0xFFFF0000) | ((b << 8) & 0xFF00) | (C  & 0xF);

On this solution, bits that can overflow are ignored.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 36594215
This is not schoolwork
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 36597162
x77 accidentally typed 15 in his first solution. The actual solution would be his second one:
return (A << 16) | (B << 8) | C;
Which would work if A, B, and C were in the right range. To guarantee that B and C don't mess things up you could do
return (A << 16) | ((B&255) << 8) | (C&15);

This would ensure that no part of B overwrites A and no part of C overwrites the part that must be 0s. If you prefer hex notation (which I do) then use:

return (A << 16) | ((B&0xFF) << 8) | (C&0x0F);
The bits 4-7 that need to be 0s will be 0 from B's shift so it will work no matter what you put in for a, b, and c.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question