DEV Community


Posted on


C/C++ hacks, part 1

Even though C++ has gone too far these days by having several conventions like c++ 11/13 etc, programmers do not tend to use the new in-built library function, instead, they like to have their own logic or simple implementation. I would like to highlight some of the handy logic that all C/C++ programmers can use.

Find the given number is odd or even, you can simply use the AND operator to see output bit 0 or not

if it is 0 even
if it is 1 odd

#include <iostream>

using namespace std;

int main(int argc, char **argv)
     // find the number of even or odd
    int iNumber=101;
    if(iNumber & 1)

Let's assume, you are working with some protocol and need to build a raw buffer composed with tag and data (even your data is int/float/double, it will be written as serious by bytes)

Now, as per the protocol definition, the tag must be maximum 2 bytes, so we can hold maximum tag data like 65535

Alt Text

As per our protocol convention, we are going to assemble 2 bytes for a tag, and then 4 bytes for data length, finally actual data buffer.

The application which receives this data can easily extract the data by do the following

First, read the two bytes, then convert to tag
Then read 4 bytes to read, how much data is resided in the frame
Using the above length parameter, it can extract data.

So, how do we compose this frame

char *_zBuf ; // assume, it is already allocated
unsigned short usTag = (unsigned short) _iTag;

// get the first byte
((unsigned char*) _zBuf)[1] = (unsigned char) usTag & 0x00FF;
usTag >>= 8;
// get the second byte
((unsigned char*) _zBuf)[0] = (unsigned char) usTag & 0x00FF;

// now we need to write the length
unsigned int usDataLength = (unsigned int) _zData.length();

uint32_t uiLenConverted = htonl(usDataLength);
memcpy(_zBuf + 2, &uiLenConverted, sizeof(uint32_t));
// now write the data
memcpy(_zBuf + 6, _zData.length(), _zData.c_str());

XOR operator has many use cases in C++ due to the following properties

The XOR of a number with itself is zero.
A ^ A = 0

The XOR of a number with zero is the number itself.
A ^ 0 = A

The order in which you do XOR does not matter.
A ^ B = B ^ A

Assume the following use case
The set is given with a series of integers, where all the numbers are placed evenly except one. Find that number?

for example array[5] = {10,10,80,14,14} , answer should 80

#include <iostream>

int FindOddOccurance(int arr[], int n)
    int res = 0, i;
    for (i = 0; i < n; i++)
// using above mentioned XOR properties.
        res ^= arr[i];
        return res;

int main(void)
    int arr[] = { 10,10,80,14,14 };
    int n = sizeof(arr) / sizeof(arr[0]);
    std::cout<<"The odd occurring element is :"<<FindOddOccurance(arr, n)<<std::endl;
    return 0;

Writing part 2...

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git