CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Computer Concepts : Dealing with Bits, Bytes and Words

In the previous Article, “Computer Concepts : Bits, Bytes and Words“, I explained the concepts of Bits, Bytes, and Words. In this article, I am going to explain, through sample programs, dealing with Bits, Bytes, and Words.

Bits

“C” Language, provides bit-wise operators to directly deal with Bits. Mostly bit-wise operations are helpful in Data Compression Techniques, Computer Graphics, Complex Data Structures, etc..

bit-wise operators, directly work on bits. For example, the bit-wise AND “&” operator; applies on each bit in the given operands and results in 1 if both bits are 1; otherwise, it results in 0. Remember that, this is directly applied to the bits; to speed up the computation.

For example, take 2 operands; the values 2 and 3. Lets’ apply bit-wise AND “&”: First we need to convert the operands into binary format.

The binary representation of 2 is 10

The binary representation of 3 is 11

After applying bit-wise AND “&”, the result would be 10 which is 2.

Lets’ look at the below program and its’ result:

#include <stdio.h>

void main()
{
printf("Bit-wise AND (2&3) = %d\n", (2&3));
printf("AND (2&&3) = %d\n", (2&&3));
}

Once you compile & run this program. You will see the below result:

[malin@localhost Projects]$ gcc bits.c
[malin@localhost Projects]$ ./a.out
Bit-wise AND (2&3) = 2
AND (2&&3) = 1

Observe that, bit-wise, AND produced a different result than the AND (“&&”) operator. Other than bit-wise AND (“&”), “C” provides, other useful bit-wise operators: like bit-wise OR(“|”), left shift (“<<“), right shift (“>>”), etc,. All these are very useful when dealing with bits.

Bytes

“Bytes” is a widely used term in Computer Science. Mostly the data we deal, in bytes. Size of the file in bytes, size of the memory in bytes, transfer of the data in bytes, etc.

When we store the data in the Storage; the data will be stored in Bytes. File Input / Output operations are mostly done by using Bytes. Read “N” bytes from the File; or Write “N” Bytes to the File; Transfer “N” Bytes over the Network etc.

For example, the below program will read “N” bytes from the file & display “N” bytes on the screen.

// bytes.c
#include <stdio.h>

void main()
{
   char bytes[512] = "\0";

   FILE *fp = fopen("./bytes.c", "r");
   if ( fp != NULL )
   {
      while ( fread(bytes, sizeof(char), 512, fp) > 0 )
      printf(bytes);

      fclose(fp);
   }
}

Save the above code into “bytes.c”. Compile and Run the code, and you will see that; the same code is displayed on the screen. The code opens the file “bytes.c” and displays its contents on the screen.

WORDs

Mostly we use this term in CPU Registers. WORDs represent the size of the Register. The Register size is depending on the design of the System. For 32-bit Systems, usually, the WORD size is 32-bits; and we will have 32-bit Registers.

Lets’ give one example, here to access the values from the Registers EAX, EBX, ECX & EDX registers (these are 32-bit). We need to do a lot more, to deal with this directly Registers; here I am giving a simple example. This doesn’t do much, but simply displays the value in these registers.

// words.c
#include <stdio.h>

void main()
{
   int i = 0;

   register int eax asm("eax");
   register int ebx asm("ebx");
   register int ecx asm("ecx");
   register int edx asm("edx");

   printf("EAX: %d\nEBX: %d\nECX: %d\nEDX: %d\n", eax, ebx, ecx, edx);
}

This Article explains at a high level, the usage of Bits, Bytes & WORDs. If time permits, I will explain more about them in the coming Articles.

I hope this article helps you. If so, please give a comment.

// Malin

Computer Concepts : Dealing with Bits, Bytes and Words

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top