bit manipulation in C#

This is a discussion on bit manipulation in C# within the C# Programming forums, part of the General Programming Boards category; can someone please explain why one needs a larger data type for the result variable in C# as in the ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    bit manipulation in C#

    can someone please explain why one needs a larger data type for the result variable in C# as in the code below

    Code:
    using System;
    
    class MyClass {
    
             public static void Main() {
             byte varA=10;// binary equivalent for 10 is 00001010
             byte varB=20;// binary equivalent for 20 is 00010100
             int result=varA & varB; // AND operation result should be 00000000
      }
    the result of any logical operation never results in a larger number, why the need for one? i tried to compile using result as byte and got "Cannot implicitly convert type 'int' to 'byte'" error.


    luigi
    Last edited by luigi40; 06-20-2005 at 07:50 AM.

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    An int is 4bytes, while your byte is only, well, a byte? I think your trying to store too little information in the "result" variable. Try changing result from type "int" to type "byte" and see if it works.

    Someone correct me if I'm wrong on the above logic.
    To code is divine

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I believe he said he'd tried with Byte but had to use an int?

    Could just be something funny with the & operator. Division of one int against another int rsults in an int by default when sometimes you want a double etc, this may be the opposite case. If you know this to be safe, you should be able to do this:

    byte result = (byte)(varA & varB);

    Although the extra casting can have a performance penalty.

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by stovellp
    I believe he said he'd tried with Byte but had to use an int?

    Could just be something funny with the & operator. Division of one int against another int rsults in an int by default when sometimes you want a double etc, this may be the opposite case. If you know this to be safe, you should be able to do this:

    byte result = (byte)(varA & varB);

    Although the extra casting can have a performance penalty.
    Oh sorry, I had read that wrong; stupid me.
    To code is divine

  5. #5
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    ty

    thanks for your replies guys, i have experimented in debug mode with numbers less than 255 and always get the correct result using ints so im going to use ints, i think ints are the default data types in .NET


    luigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 11:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21