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

  • Context: C# 
  • Thread starter Thread starter hadi amiri 4
  • Start date Start date
  • Tags Tags
    Code Explain
Click For Summary

Discussion Overview

The discussion revolves around understanding a specific C# code snippet that manipulates bits within a byte, with participants seeking clarification on its functionality and potential C equivalents. The scope includes conceptual understanding of bitwise operations and their applications in programming.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks for an explanation of the C# code and its equivalent in C.
  • Another participant suggests that the code appears to be related to homework and questions its purpose.
  • A participant claims that the C equivalent of the C# code is nearly identical, suggesting a type change from "byte" to "char" and a modification from a reference to a pointer.
  • One participant recommends researching "bitwise operators" to understand the operations used in the code, noting that C's bitwise operators are similar to those in C#.
  • A participant explains that the functions are used for embedding and extracting bits from a byte, specifically in the context of steganography using the least significant bit (LSB) method in 24-bit BMP images, and requests examples for better understanding.
  • Another participant provides a detailed explanation of how the functions operate, describing the bit manipulation processes involved in the Replace and Extract functions, while encouraging further reading on bitwise arithmetic.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and interest in the code, with some providing explanations and others seeking clarification. No consensus is reached on a definitive understanding or application of the code.

Contextual Notes

Some participants note the need for examples to clarify the functions' operations, indicating that further context may be necessary for complete comprehension.

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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K