C# Can anyone explain what this c# code does or give a C equalivent?

  • Thread starter Thread starter hadi amiri 4
  • Start date Start date
  • Tags Tags
    Code Explain
AI Thread Summary
The discussion revolves around two C# functions, `Replace` and `Extract`, which manipulate individual bits within a byte. The `Replace` function sets or clears a specific bit based on the provided position and value, while the `Extract` function retrieves the value of a bit at a specified position. The C equivalent of the C# code involves changing the `byte` type to `char` and using pointers instead of references. Both functions utilize bitwise operators, which are consistent between C# and C. The primary application of these functions is in steganography, particularly for manipulating the least significant bits (LSB) in 24-bit BMP images. Understanding these functions requires familiarity with bitwise arithmetic, which is fundamental in programming for bit manipulation tasks.
hadi amiri 4
Messages
98
Reaction score
1
C# vs c !

Hello every body!

can anyone explain what this c# code does or give a C equalivent?

public static void Replace(ref byte b, int pos,
byte value)
{
b = (byte) (value == 1 ? b | (1 << pos)
: b & ~(1 << pos));
}

public static byte Extract(byte b, int pos) {
return (byte) ((b & (1 << pos)) >> pos);
}
 
Technology news on Phys.org


This looks like homework. What do you think it does?
 


can anyone explain what this c# code does or give a C equalivent?

The C code is essentially identical to the C# code given. Just replace the "byte" type with "char", and change the first parameter of Replace from a reference to a pointer.
 


If you want to understand this I suggest you google "bitwise operator". These are the |, &, ~ sort of operators and the C bitwise operators are the same in C#.
 


These two functions are used for empadding and extracting the bits from a byte ,they are used for steganography with LSB method in 24-bit .bmp pics,i just wanted to kmow that how they work ,i mean with some examples.can anyone help?:biggrin:
 


Well they're just simple functions to treat a byte like an array of bits and get or set single bit values out. So if you say Replace(ref x, 4, 1) you are setting the 5th most significant bit of the x variable with "1".

Extract(b,pos) in english says shift a 1 value up by pos, bitwise-and the result by b, then shift the value back down by pos (so that the only possible end results are 0 and 1). Replace(b,pos,value) says take the value referenced as b and if the value is 1 then bitwise-or the value by 1 shifted up by pos, otherwise bitwise-and with a mask which is the NOT of 1-shifted-up-by-pos. This is all pretty basic stuff so again I urge you to read up on bitwise arithmetic. Basically any introduction to the subject will cover the stuff happening in these functions.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...

Similar threads

Replies
22
Views
3K
Replies
9
Views
2K
Replies
16
Views
3K
Replies
11
Views
2K
Replies
3
Views
13K
Replies
7
Views
1K
Replies
7
Views
3K
Back
Top