Jump to content

new in C#

- - - - -

  • Please log in to reply
8 replies to this topic

#1
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
Hi

I have never deal with C# before ,,, That is why my question will look silly :crying:

I have this code from the web:

byte waveByte = waveBuffer;


now I don't now how C# deals with byte ,,,

what I want to do is change one bit from wave Byte

example :
waveByte =16=10000
then I want to change the red bit to 1
=10000 = 10100

HOW???

Thank you so much

#2
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
:crying::crying::crying::crying::crying::crying:

#3
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
pleaaaaaaaase anyone....

may be I can convert it to string first -----> change that bit ----------> convert it back to byte ????

is this will work???

help please

#4
NightmareZ

NightmareZ

    Newbie

  • Members
  • Pip
  • 1 posts

prof.deedee said:

waveByte =16=10000
then I want to change the red bit to 1
=10000 = 10100

byte waveByte = 16;

waveByte = (byte)(waveByte | 4);

// waveByte = 20 (bin: 10100)

Or.... I wrote simple method for this:
static byte ChangeBit(byte source, byte bitNumber, byte value)

{

    if (bitNumber > 8 || (value != 0 && value != 1))

        throw new ApplicationException("Invalid arguments");


    bitNumber = (byte)(Math.Pow(2, bitNumber));


    if (value == 0)

        return (byte)(source & ~bitNumber);

    else

        return (byte) (source | bitNumber);

}
,
where:
source - old value,
bitNumber - number of bit (starts from 0), for your example is 2
value - target value for bit (0 or 1)

Using:
byte waveByte = 16;

waveByte = ChangeBit(waveByte, 2, 0);

// waveByte = 20 (bin: 10100)


Sorry for my beginner's English.

#5
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
Thank you soo much

#6
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
can I change it to String ---->change the bit ------>change it back to byte????

#7
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
sorry NightmareZ what this line suppose to do:

Quote

(byte)(source & ~bitNumber);


and this one:

Quote

(byte) (source | bitNumber);


#8
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
why change it to a string? All you have to do is use the | (or) operator to change the bit or byte from 0 to 1

int n = 10000;

int mask = 100;

n = n | mask;


// or you could do this

n = n | 100;


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#9
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
Thank you that is really work




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users