Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Feb 12, 2009

how to reverse Lower nibble and higher nibble in a byte.

how to reverse Lower nibble and higher nibble in a byte.
b = (b >> 4) | (b << 4) ; / if b is unsigned
b = ((b >> 4)&15) | (b << 4) ;//if b is signed

memory address queation

#include
main()
{
char a[]="Hello";
char b[]="Hello";

char *p="hi";
char *q="hi";

if(a==b)
printf("Both are equal\n");
else
printf("Not equal\n");

if(p==q)
printf("Both are equal %c",*p);
else
printf("Not equal\n");
}
Not equal for the first. The two arrays are allocated separately, and the equality operator tests for whether the pointer points to the same address in memory. Two arrays in different places means that is a no.
The second, however, does produce equal pointers on my default version of gcc (I suspect there are some compilers and/or optimization settings for which this would NOT be true). The reason is because the compiler has optimized the common string literal in both places by storing it at one place in memory. That place is incidentally within the program's code segment, which means if you try touching it BAM segfault for you.

Feb 5, 2009

how to determine the size of the array?

how to determine the size of the array?
{{{
int a[10];
sizeof(a)/sizeof(int);
}}}

Jan 15, 2009

coding

typedef int aaa, bbb, ccc;
typedef int ar[15], arr[9][6];
typedef char c, *cp, carr[100];

/* now declare some objects */

/* all ints */
aaa int1;
bbb int2;
ccc int3;

ar yyy; /* array of 15 ints */
arr xxx; /* 9*6 array of int */

c ch; /* a char */
cp pnt; /* pointer to char */
carr chry; /* array of 100 char */

coding

// Works with acquire/release semantics for volatile
// Broken under Java 1.4 and earlier semantics for volatile
class Foo {
private volatile Helper helper = null;
public Helper getHelper() {
if (helper == null) {
synchronized(this) {
if (helper == null)
helper = new Helper();
}
}
return helper;
}
// other functions and members...
}
http://en.wikipedia.org/wiki/Double-checked_locking

Jan 11, 2009

Write a function to implement a ternary operator a?b:c without using any conditional statements like if,for,while etc.. you are allowed to you any ope

Write a function to implement a ternary operator a?b:c
without using any conditional statements like if,for,while etc..
you are allowed to you any operators like &, |, ^ etc...
your function can use return statement.

int Ternary(int a, int b, int c)
{
return (a&b)|(!a&c);
}

Write a c function for finding minimum of two numbers. You should not use if-then-else or ?: operators

Write a c function for finding minimum of two numbers. You should not use if-then-else or ?: operators

int min (int a, int b)
{
return ((a > b) * b + (a < b) * a);
}

Jan 6, 2009

In the class define, if you do not want client to use member function which can be defined implicitly by compiler like operator=, you can explicitly d

In the class define, if you do not want client to use member function which can be defined implicitly by compiler like operator=, you can explicitly declare this member function as private and do not give a definition to it.

const variable

int main(){
const int var=15;
int *p;
p=(int*)&var;
*p=10;
printf("%d %d\n",var,*p);
cout<<&var<<" "<<p<<endl;
}
15 10
0012FED4 0012FED4
I think compiler do some optimization on const variable
By the way. This is undefined

one way to allocate 2 dimensional array in contiguous memory chunk

one way to allocate 2 dimensional array in contiguous memory chunk

int main(){
int row=3,col=4;
int i,j;
int (*p)[col];
p=malloc(row *sizeof(*p));
}

int main(){
int row=3,col=4;
int i,j;
int (*p)[row][col];
p=malloc(sizeof(*p));
}