cByte

Overview

Module with functions and types to work with byte values.

Types and Definitions

cByte

typedef uint8_t cByte;

cByte is an alias for uint8 and is equivalent to uint8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.

c_check_byte

typedef bool ( *c_check_byte )( cByte b );

c_check_byte is the signature of a function to check a byte value.

Functions

overall

byte_c_

#define byte_c_( Value )

Macro function that casts the Value as cByte.

cmp_byte_c

int cmp_byte_c( cByte a, cByte b );

Compares two cByte values and returns the three possible results:

<0

means that a is less compared to b

0

means that a and be are equal

>0

means that a is greater compared to b

Example
Unresolved directive in cByte.adoc - include::../../../test/clingo/type/cByte/cmp_byte.c[]

bit

byte_as_bit_c

cByte byte_as_bit_c( cByte byte );

Trims the byte to the least significant bit.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cByte.h"

int main( void )
{
   init_tap_c_();

   expect_c_( byte_as_bit_c( 0 ) == 0 );
   expect_c_( byte_as_bit_c( 1 ) == 1 );

   expect_c_( byte_as_bit_c( '0' ) == 0 ); // ascii 30
   expect_c_( byte_as_bit_c( '1' ) == 1 ); // ascii 31

   expect_c_( byte_as_bit_c( 2 ) == 0 );
   expect_c_( byte_as_bit_c( 3 ) == 1 );

   return finish_tap_c_();
}

get_byte_bit_c

cByte get_byte_bit_c( cByte byte int64_t pos );

Returns the bit at pos in a byte.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cByte.h"

int main( void )
{
   init_tap_c_();

   cByte byte = 0xaa; // 1010 1010
   expect_c_( get_byte_bit_c( byte, 0 ) == 1 );
   expect_c_( get_byte_bit_c( byte, 1 ) == 0 );
   expect_c_( get_byte_bit_c( byte, 4 ) == 1 );
   expect_c_( get_byte_bit_c( byte, 7 ) == 0 );

   return finish_tap_c_();
}

set_byte_bit_c

cByte set_byte_bit_c( cByte byte, int64_t pos, cByte bit );

Sets the bit at pos in a byte.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cByte.h"

int main( void )
{
   init_tap_c_();

   cByte byte = 0xaa; // 1010 1010

   expect_c_( set_byte_bit_c( byte, 0, 0 ) == 0x2a ); // 0010 1010
   expect_c_( set_byte_bit_c( byte, 1, 1 ) == 0xea ); // 1110 1010
   expect_c_( set_byte_bit_c( byte, 7, 0 ) == 0xaa ); // 1010 1010
   expect_c_( set_byte_bit_c( byte, 7, 1 ) == 0xab ); // 1010 1011

   return finish_tap_c_();
}

toggle_byte_bit_c

cByte toggle_byte_bit_c( cByte byte, int64_t pos );

Toggles the bit at pos in a byte.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cByte.h"

int main( void )
{
   init_tap_c_();

   cByte byte = 0xaa; // 1010 1010

   expect_c_( toggle_byte_bit_c( byte, 0 ) == 0x2a ); // 0010 1010
   expect_c_( toggle_byte_bit_c( byte, 1 ) == 0xea ); // 1110 1010
   expect_c_( toggle_byte_bit_c( byte, 4 ) == 0xa2 ); // 1010 0010
   expect_c_( toggle_byte_bit_c( byte, 7 ) == 0xab ); // 1010 1011

   return finish_tap_c_();
}