Sponsored Content
Top Forums Programming Comparing unsigned char bits. Post 302624759 by agama on Monday 16th of April 2012 07:48:37 PM
Old 04-16-2012
Here is a quick example of setting and checking a bit vector based on 255 possible combinations:

Code:
#include "stdio.h"
#include "stdlib.h"

#define VSIZE  256/8            // vector size in bytes
void mark( unsigned char *vector, unsigned char byte )
{
    int idx;
    int mask;

    idx = byte / 8;
    mask = 1 << (byte % 8);

    vector[idx] |= mask;
}

int check( unsigned char *vector, unsigned char byte )
{
    int idx;
    int mask;

    idx = byte / 8;
    mask = 1 << (byte % 8);

    return vector[idx] & mask;
}

int main( int argc, char **argv )
{
    unsigned int    value;
    unsigned char vector[VSIZE];

    printf( "enter non-number or ctl-D to quit\n" );
    for( ; ; )
    {
        printf( "enter a number (0-255): " );
        if( scanf( "%u", &value ) <= 0 )        // read value converting it to unsigned
        {
            printf( "\n" );
            exit( 0 );
        }
        if( value < 256 )
        {
            printf( "the value %u has %sbeen entered before\n", value, check( vector, value ) ? "" : "not " );
            mark( vector, value );
        }
        else
            printf( "%d is out of range for a byte\n", value );
    }

    return 0;
}


Last edited by agama; 04-16-2012 at 09:11 PM.. Reason: typo
 

8 More Discussions You Might Find Interesting

1. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies

2. UNIX for Dummies Questions & Answers

Changing 24 bits to 8 bits display

Hello all, I was wondering if anyone can tell me how to change 24 bits depth display to 8 bits depth display for Sun Ultra1, running Solaris 8? THANKS in advance. I think that the command is ffbconfig, but it has nothing about depth. (4 Replies)
Discussion started by: larry
4 Replies

3. Programming

to get the correct value with unsigned int

hi, Please help me with the following code to get the difference in values. struct a{ int b1; int c1; char d1; } main() { unsigned int b=10; unsigned int c; c = b - (unsigned int )sizeof(a); printf("%d",c); } Here c returns some junk value. How can i get the... (2 Replies)
Discussion started by: naan
2 Replies

4. UNIX for Dummies Questions & Answers

32 bits procesaor with 64 bits Solaris

people i have a problem i have a 32 bits sparc processor, and solaris 64 bits processor, i install a oracle data base 64 bits, but my oracle will not run because my processor is from 32 bits this is ok??, i know if i have x86 i cannot install a 64 bits operatin system in a 32 bits processor. ... (0 Replies)
Discussion started by: enkei17
0 Replies

5. Programming

Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables?? When I declare a variable "unsigned;" what did I do it??? Why would I delcare an integer "long" or "short" ( unsigned or signed)?? Any examples of when things like "unsigned", "long", "short" etc...... (6 Replies)
Discussion started by: cpp_beginner
6 Replies

6. Programming

Signed and unsigned intergers

when a date type is considered signed and unsigned is that simple referring to - for signed and positive numbers for unsigned? Further if that is the case would mutiplying and dividing ect where 2 signed numbers, like (-2)*(-2) = 4 result in a unsigned. (3 Replies)
Discussion started by: Fingerz
3 Replies

7. What is on Your Mind?

Place Bits & Win Bits!!! - 17th Annual Satellite Awards

Ten movies have been nominated as best motion picture by the International Press Academy, presentation of the 2012 Satellite Awards will be held on 16th December at Los Angeles, CA. Place your bits here on one of the below nominated movie of your choice:- Argo ... (0 Replies)
Discussion started by: Yoda
0 Replies

8. OS X (Apple)

Unsigned to signed, error?...

Hi guys... Macbook Pro, 13", circa August 2012, OSX 10.7.5, default bash terminal. I require the capability to convert +32767 to -32768 into signed hex words... The example piece code below works perfectly except... #/bin/bash # sign.sh # Unsign to sign... while true do # I have used... (2 Replies)
Discussion started by: wisecracker
2 Replies
DPGEN_ADSB2F(3) 					  Alliance - genlib User's Manual					   DPGEN_ADSB2F(3)

NAME
DPGEN_ADSB2F - Adder/Substractor Macro-Generator SYNOPSIS
#include <genlib.h> void GENLIB_MACRO (DPGEN_ADSB2F, char *modelname, long flags, long N); DESCRIPTION
Generate a N bits adder/substractor named modelname. How it works : o if the add_sub signal is set to '0' an addition is performed, otherwise it's a substraction. o Operation can be either signed or unsigned. In unsigned mode c31 is the overflow, in unsigned mode you have to compute overflow by XORing c31 and c30. TERMINAL NAMES 1. add_sub : select addition or substraction (input, 1 bit). 2. c31 : carry out. In unsigned mode, this is the overflow (output, 1 bits). 3. c30 : used to compute overflow in signed mode : overflow := c31 xor c30 (output, 1 bits). 4. i1 : first operand (input, N bits). 5. i0 : second operand (input, N bits). 6. q : output (N bits). 7. vdd : power. 8. vss : ground. EXAMPLE
GENLIB_MACRO(DPGEN_ADSB2F, "model_adsb2f_32" , F_BEHAV|F_PLACE , 32 ); GENLIB_LOINS( "model_adsb2f_32" , "instance1_adsb2f_32" , "add_sub" , "c32" , "c31" , "i1[31:0]" , "i0[31:0]" , "q[31:0]" , "vdd", "vss", NULL ); SEE ALSO
GENLIB_MACRO(3), genlib(1) ASIM
/LIP6 30 July 2004 DPGEN_ADSB2F(3)
All times are GMT -4. The time now is 01:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy