Solved

C# Bit Shifter

Posted on 2011-09-23
8
327 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
Comment Utility
  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
Comment Utility
Sorry, validation  comparison must be  ">="
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

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

Expert Comment

by:x77
Comment Utility
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
Comment Utility
This is not schoolwork
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now