Journal Articles

CVu Journal Vol 12, #5 - Sep 2000 + Letters to the Editor
Browse in : All > Journals > CVu > 125 (21)
All > Journal Columns > LettersEditor (132)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: The Wall

Author: Administrator

Date: 08 September 2000 13:15:39 +01:00 or Fri, 08 September 2000 13:15:39 +01:00

Summary: 

Body: 

Undefined?

Dear Francis,

One Friday afternoon, I was playing around with writing useful little utilities and ended up writing the following 'optimised' code; a test in operator precedence maybe :-)

#include <stdio.h>
#include <stdlib.h>
static void p(int v, int b) {
  do {
    int c = (b-,8);
    do 
      putchar(v & 1 <<b * 8+ -c ? '1' : '0')
        , b|c && c%4 != 0||putchar(' ');
    while (c);
  }
  while (b);
}

int main(int c, char** a) {
  while (-c) {
    int v = strtol(* ++a, 0, 0), 
      b = 1<<(v >> 16 != 0)+(v>>8 != 0);
    printf("dec=%d hex=%#0*x bin=",v,2+2*b,v)
      , p (v, b), putchar('\n');
  }
  return c;
}

While I include this for your amusement, in trying to optimise it further, I did stumble upon writing:

int i = i ^ i;

My question would be, out of purely satanic interest is does this produce undefined behaviour or not? Since I'm not well versed in the standard, I thought I'd ask the expert :-)

Do your employers know you have this addiction? When the compiler has seen the declaration part of the statement, 'i' is in scope. As the evaluation of the initialisation expression requires that i be read before it has been initialised, you have undefined behaviour. While most implementations use all the bit patterns to represent values, some do not have what are called trap values. The result of the contents of i being initially a trap value would result in behaviour outside the scope of the Standards for both C and C++.

Note that there is no requirement that the compiler can analyse your code to the extent of evaluating the expression, so it is no use arguing that the compiler can know that the expression necessarily evaluates to 0.

I hope that helps.

Notes: 

More fields may be available via dynamicdata ..